Fill arrays with ranges of numbers
Is there any syntax/package allowing quick filling of java arrays with ranges of numbers,开发者_开发百科 like in perl?
e.g.
int[] arr = new int[1000];
arr=(1..500,301..400,1001..1400); // returns [1,2,3,4,...,500,301,302,...,400,1001,1002,...1400]
Also, it here a package that allows getting the n-th number in such list of numbers as the above, without actually creating the array (which can be huge)?
e.g.
BunchOfRangesType bort = new BunchOfRangesType("1..500","301..400","1001..1400");
bort.get(0); // return 1
bort.get(500); // return 301
bort.get(501); // return 302
It's not too difficult to implement, but I guess it might be common so maybe it was already done.
For those still looking for a solution:
In Java 8 or later, this can be answered trivially using Streams without any loops or additional libraries.
int[] range = IntStream.rangeClosed(1, 10).toArray();
This will produce an array with the integers from 1 to 10.
A more general solution that produces the same result is below. This can be made to produce any sequence by modifying the unary operator.
int[] range = IntStream.iterate(1, n -> n + 1).limit(10).toArray();
There is dollar:
// build the List 10, 11, 12, 13, 14
List<Integer> list2 = $(10, 15).toList();
maven:
<dependency>
<groupId>org.bitbucket.dollar</groupId>
<artifactId>dollar</artifactId>
<version>1.0-beta3</version>
</dependency>
Another useful and not widely known Java 8 solution for existing arrays:
int[] array = new int[10];
Arrays.setAll(array, i -> i + 1);
As for the first question, whether it is possible to fill an array with the values of a range: it is actually possible to achieve that with the combination of Range, DiscreteDomain, ContiguousSet and Ints from Guava:
int[] array = Ints.toArray(
ContiguousSet.create(Range.closed(1, 500), DiscreteDomain.integers()));
And, not exactly what is mentioned in the second part of the question, but it is possible to create a set with the elements of a range of a discrete domain:
Set<Integer> numbersFrom1To500 =
ContiguousSet.create(Range.closed(1, 500), DiscreteDomain.integers());
The resulting Set
will not contain the specified elements physically, only logically (so it's memory footprint will be small), but can be iterated (since it's a Set
):
for (Integer integer : numbersFrom1To500) {
System.out.println(integer);
}
Not quite as clean as True Soft's answer, but you can use Google Guava to the same effect:
public class Test {
public static void main(String[] args) {
//one liner
int[] array = toArray(newLinkedList(concat(range(1, 10), range(500, 1000))));
//more readable
Iterable<Integer> values = concat(range(1, 10), range(500, 1000));
List<Integer> list = newLinkedList(values);
int[] array = toArray(list);
}
public static List<Integer> range(int min, int max) {
List<Integer> list = newLinkedList();
for (int i = min; i <= max; i++) {
list.add(i);
}
return list;
}
}
Note you need a few static imports for this to work.
List<Integer> arrayOfRange = new ArrayList<Integer>();
int[] range = IntStream.iterate(1, n -> {arrayOfRange.add(n);return n + 1;}).limit(10).toArray();
// in addition to what craig answer if you want to have Integer 2nd approch
List<Integer> list = IntStream.of(range).boxed().collect(Collectors.toList());
public static int[] getRangeArray(int start, int end){
final int[] rangeArray = new int[end-start + 1];
for(int i = 0; i < rangeArray.length; i++){
rangeArray[i] = start + i;
}
return rangeArray; // It includes start and end values as well, pls adjust according to your need
}
精彩评论