int n = 5;
int[] oneD = new int[5];
int[] oneD2 = new int[5];
int[] oneD3 = new int[5];
.
.
.
n
int[][] twoD = new int[n][5];如何复制java中的三个oned数组来分离2D数组的行?实际上,在java 8+中是否有一些简短而方便的特性来做到这一点呢?
发布于 2020-08-11 19:37:20
有两种选择:
int =新int { oned,oned2,oned3 };
或:
twod = oned;twod1 = oned2;twod2 = oned3;
例如,twod[1][3]和oned2[3]现在引用相同的值,因此更改一个值可以更改另一个.。
System.arraycopy(oned,0,twod,0,5);System.arraycopy(oned2,0,twod1,0,5);System.arraycopy(oned3,0,twod2,0,5);
twod现在完全独立于其他数组。
发布于 2020-08-11 20:14:37
这里是您的Java 8解决方案。它只是创建新的数组并将它们组合成一个2D数组。二维阵列独立于原始阵列。感谢Andreas的int[]::clone提示。
int n = 5;
int[] oned = new int[5];
int[] oned2 = new int[5];
int[] oned3 = new int[5];
.
.
.
n
int[][] twod = Stream.of(oned, oned2, oned3,...,onedn)
.map(int[]::clone)
.toArray(int[][]::new);https://stackoverflow.com/questions/63365107
复制相似问题