gpt4 book ai didi

java - 将 Array 对象转换为该对象内的变量数组

转载 作者:行者123 更新时间:2023-12-01 06:28:31 26 4
gpt4 key购买 nike

基本上我想做的是:

Point[] points = new Point[]{new Point(2, 3), new Point(7,8), new Point(1, 8)};
int[] xCoords = new int[points.length];

for (int i = 0; i < points.lenght; i++) {
xCoords[i] = points[i].x;
}

所以最终我会得到 xCoords 看起来像这样:

{2,7,8}

是否可以以更通用的方式存档?

最佳答案

你可以做

int[] xCoords = Stream.of(points).mapToInt(p -> p.x).toArray();

关于java - 将 Array 对象转换为该对象内的变量数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36210760/

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