从二维数组中的所有值填充一维数组

假设我有一个带有 int 值的二维数组,我想用二维数组中的所有 int 值填充一个一维数组,我该怎么做呢?


这是我尝试过的,但是出了点问题,我不知道是什么..


int[][] twoDimensionalArray = {{5, 2, 3, 1},

                               {4, 2, 6, 9},

                               {8, 9, 1, 8}};

int[] oneDimensionalArray = new int[twoDimensionalArray.length * twoDimensionalArray.length];

for (int i = 0; i < twoDimensionalArray.length; i++) {

    for (int j = 0; j < twoDimensionalArray.length; j++) {

      oneDimensionalArray[i] = twoDimensionalArray[i][j];

     }

}

谢谢!


呼啦一阵风
浏览 147回答 4
4回答

猛跑小猪

我可以提出这样的建议,String[][] my2Darr = {{5, 2, 3, 1},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {4, 2, 6, 9},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {8, 9, 1, 8}};&nbsp; &nbsp; List<String> list = new ArrayList<>();&nbsp; &nbsp; for(int i = 0; i < my2Darr.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; list.addAll(Arrays.asList(my2Darr[i])); // java.util.Arrays&nbsp; &nbsp; }&nbsp; &nbsp; String[] my1Darr = new String[list.size()];&nbsp; &nbsp; my1Darr = list.toArray(my1Darr);爪哇 8:int[][] 2darr = {{5, 2, 3, 1},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{4, 2, 6, 9},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{8, 9, 1, 8}};int[] 1darr = Stream.of(2darr ).flatMapToInt(IntStream::of).toArray();System.out.println(Arrays.toString(1darr ));

杨__羊羊

int[][] twoDimensionalArray = {{5, 2, 3, 1},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{4, 2, 6, 9},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{8, 9, 1, 8}};int[] oneDimensionalArray = new int[twoDimensionalArray.length * twoDimensionalArray.length];for (int i = 0; i < twoDimensionalArray.length; i++) {&nbsp; &nbsp; for (int j = 0; j < twoDimensionalArray[0].length; j++) {&nbsp; &nbsp; &nbsp; oneDimensionalArray[i] = twoDimensionalArray[i][j];&nbsp; &nbsp; &nbsp;}}您的解决方案几乎是正确的。您必须更正twoDimensionalArray.length为twoDimensionalArray[0].length. 因为你想水平和垂直迭代。如果您在 twoDimensionalArray.length 上迭代 2 次,您将迭代 2 倍水平轴的长度。

函数式编程

您可以使用它,它用于System.arraycopy()将内部数组复制到结果中:public static int[] flatten(int[][] input) {&nbsp; &nbsp; int length = input.length;&nbsp; &nbsp; if(length == 0) return new int[0];&nbsp; &nbsp; int[] output = new int[length * input[0].length];&nbsp; &nbsp; for(int i = 0; i < length; i++) {&nbsp; &nbsp; &nbsp; &nbsp;int[] inner = input[i];&nbsp; &nbsp; &nbsp; &nbsp;// 1. Parameter: the source to copy from&nbsp; &nbsp; &nbsp; &nbsp;// 2. Parameter: the starting index from source&nbsp; &nbsp; &nbsp; &nbsp;// 3. Parameter: the destionation to copy to&nbsp; &nbsp; &nbsp; &nbsp;// 4. Parameter: the starting index from destination&nbsp; &nbsp; &nbsp; &nbsp;// 5. Parameter: the amount of elements to copy&nbsp; &nbsp; &nbsp; &nbsp;System.arraycopy(inner, 0, output, i * length, inner.length);&nbsp; &nbsp; }&nbsp; &nbsp; return output;}

慕田峪7331174

您可以使用 来执行此操作stream,例如:int[][] twoDimensionalArray = {{5, 2, 3, 1},&nbsp; &nbsp; &nbsp; &nbsp; {4, 2, 6, 9},&nbsp; &nbsp; &nbsp; &nbsp; {8, 9, 1, 8}};int[] oneDimensionalArray = Arrays.stream(twoDimensionalArray)&nbsp; &nbsp; .flatMapToInt(e -> Arrays.stream(e))&nbsp; &nbsp; .toArray();System.out.println(Arrays.toString(oneDimensionalArray));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java