java如何使用map函数将point3d数组转换成double类型的数组
时间: 2024-04-30 22:24:53 浏览: 72
在 Java 中,我们可以使用 `Arrays.stream()` 方法将数组转换为流,然后使用 `map()` 方法将每个 `Point3D` 对象转换为对应的 `double` 数组,最后使用 `toArray()` 方法将流转换为 `double` 数组。下面是一个示例代码:
```java
import java.util.Arrays;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
Point3D[] points = { new Point3D(1, 2, 3), new Point3D(4, 5, 6), new Point3D(7, 8, 9) };
double[][] doubles = Arrays.stream(points)
.map(point3D -> new double[] { point3D.getX(), point3D.getY(), point3D.getZ() })
.toArray(double[][]::new);
System.out.println(Arrays.deepToString(doubles));
}
}
class Point3D {
private double x;
private double y;
private double z;
public Point3D(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getZ() {
return z;
}
public void setZ(double z) {
this.z = z;
}
}
```
输出结果为:
```
[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]
```
阅读全文