Java 用数字范围填充数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3387373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fill arrays with ranges of numbers
提问by David B
Is there any syntax/package allowing quick filling of java arrays with ranges of numbers, like in perl?
是否有任何语法/包允许使用数字范围快速填充 java 数组,例如在 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)?
此外,这里有一个包,它允许在上述数字列表中获取第 n 个数字,而无需实际创建数组(可能很大)?
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.
实施起来并不难,但我想这可能很常见,所以也许已经完成了。
采纳答案by Jared Russell
Not quite as clean as True Soft's answer, but you can use Google Guavato the same effect:
不像 True Soft 的回答那么干净,但你可以使用谷歌番石榴达到同样的效果:
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.
请注意,您需要一些静态导入才能使其工作。
回答by True Soft
回答by Katona
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, ContiguousSetand Intsfrom Guava:
至于第一个问题,是否可以用范围的值填充数组:实际上可以通过来自Guava的Range、DiscreteDomain、ContiguousSet和Ints的组合来实现:
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
):
结果Set
将不包含物理上的指定元素,仅在逻辑上包含(因此它的内存占用量很小),但可以迭代(因为它是一个Set
):
for (Integer integer : numbersFrom1To500) {
System.out.println(integer);
}
回答by Craig
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.
在 Java 8 或更高版本中,这可以使用 Streams 轻松解决,无需任何循环或其他库。
int[] range = IntStream.rangeClosed(1, 10).toArray();
This will produce an array with the integers from 1 to 10.
这将生成一个包含 1 到 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();
回答by Arun Pratap Singh
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
// 如果你想要整数 2nd approch,除了什么 craig 回答
List<Integer> list = IntStream.of(range).boxed().collect(Collectors.toList());
回答by Manos Nikolaidis
Another useful and not widely known Java 8 solution for existing arrays:
现有数组的另一个有用但不广为人知的 Java 8 解决方案:
int[] array = new int[10];
Arrays.setAll(array, i -> i + 1);