[英]Java one liner for loop
Here , someone made a one-liner for
loop in Python. 在这里 ,有人提出一个班轮for
Python中循环。
Another example is this: 另一个例子是:
someList = [f(i) for i in range(16)]
which would be a one-liner for this code: 这将是这个代码的单行:
someList = []
for i in range(16):
someList.append(f(i))
or, in Java: 或者,在Java中:
int[] someList = {}
for (int i = 0; i < 16; i++) {
someList = append(someList, f(i));
}
such that f
is some function that will return an integer. 这样f
就是一些返回整数的函数。
Now, is there an equivalent one-liner in Java? 现在,Java中有一个等效的单行程吗?
Note: Currently, I am using Processing, which is similar to Java, so, any code written in Java might be usable in Processing. 注意:目前,我使用的是与Java类似的Processing,因此,任何用Java编写的代码都可以在Processing中使用。
Java 8的IntStream
拯救:
int[] someList = IntStream.range(0, 16).map(i -> f(i)).toArray();
Traditional 传统
int [] arr = {1,4,6,7,2};
for(int i = 0; i< arr.length; i++){
System.out.println(arr[i]);
}
One Line 一条线
for (int val : arr) { System.out.println(val); }
opt: 选择:
1
4
6
7
2
1
4
6
7
2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.