Here,有人在Python中创建了一个单行for
循环。
另一个例子是:
someList = [f(i) for i in range(16)]
这将是此代码的一行代码:
someList = []
for i in range(16):
someList.append(f(i))
或者,在Java中:
int[] someList = {}
for (int i = 0; i < 16; i++) {
someList = append(someList, f(i));
}
这样f
是一个返回整数的函数。
现在,Java中有一个等效的单行程序吗?
注意:目前,我使用的是与Java类似的Processing,因此,任何用Java编写的代码都可以在Processing中使用。
答案 0 :(得分:12)
Java 8&#39;} IntStream
救援:
int[] someList = IntStream.range(0, 16).map(i -> f(i)).toArray();
答案 1 :(得分:-1)
传统
func animateIn(delay: TimeInterval = 0) {
UIView.animate(withDuration: 0.2, delay: delay, options: .allowUserInteraction, animations: {
self.shadowView.layer.shadowColor = UIColor.black.cgColor
self.shadowView.layer.shadowOffset = CGSize(width: 15, height: 15)
self.shadowView.layer.shadowRadius = 20
self.shadowView.layer.shadowOpacity = 0.2
self.layoutIfNeeded()
}, completion: nil)
}
一行
int [] arr = {1,4,6,7,2};
for(int i = 0; i< arr.length; i++){
System.out.println(arr[i]);
}
opt:
for (int val : arr) { System.out.println(val); }