gpt4 book ai didi

Java 一个 liner for 循环

转载 作者:搜寻专家 更新时间:2023-11-01 01:25:04 28 4
gpt4 key购买 nike

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 中有等效的单行代码吗?

Note: Currently, I am using Processing, which is similar to Java, so, any code written in Java might be usable in Processing.

最佳答案

Java 8 IntStream救援:

int[] someList = IntStream.range(0, 16).map(i -> f(i)).toArray();

关于Java 一个 liner for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40946871/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com