合并三个数组成为一个多维数组。

huangapple go评论77阅读模式
英文:

Merge 3 arrays into a multidimensional array

问题

我有三个单独的数组,我想将它们合并成一个多维数组。

    数组3 [1, 1, 2, 4, 1, 2, 1, 1]
    数组2 [1, 1, 2, 2, 1, 2, 6, 1]
    数组1 [1, 3, 1, 1, 1, 1, 2, 1]

我想要的输出是,所有数组都位于一个多维数组中,它们都叠放在一起。

最终数组 =

     数组3
     数组2
     数组1

我该如何实现这个?

英文:

I have three separate arrays that I would like to merge into a multidimensional array

    Array 3 [1, 1, 2, 4, 1, 2, 1, 1]
    Array 2 [1, 1, 2, 2, 1, 2, 6, 1]
    Array 1 [1, 3, 1, 1, 1, 1, 2, 1]

The output that I would like to have is where all the arrays are in one multidimensional array where they are all stacked on top of each other.

Finial Array =

     Array 3
     Array 2
     Array 1

How would I go about doing this?

答案1

得分: 1

import java.util.stream.IntStream;

public class MultiDimensionalArrayDemo {
    
    public static void main(String[] args) {
        final int ROWS = 3;
        final int COLUMNS = 10;

        int array1 [] = {  0,  1,  2,  3,  4,  5,  6,  7,  8,  9};
        int array2 [] = { 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
        int array3 [] = { 20, 21, 22, 23, 24, 25, 26, 27, 28, 29};

        int finalArray[][] =  new int[ROWS][];
        finalArray[0] = array1;
        finalArray[1] = array2;
        finalArray[2] = array3;

        System.out.println("Initial bi-dimensional array:");
        for(int i=0; i<ROWS; i++) {
            for(int j=0; j<COLUMNS; j++) {
                System.out.printf("%2d ",finalArray[i][j]);
            }
            System.out.println();
        }

        for(int i=0; i<ROWS-1; i++) {
            for(int j=0; j<COLUMNS; j++) {
                if(finalArray[i][j]==0) {
                    moveColumnUp(IntStream.range(i, ROWS).mapToObj(obj -> finalArray[obj]).toArray(int[][]::new),ROWS-i,j);
                }
            }
        }

        System.out.println("After transformation:");
        for(int i=0; i<ROWS; i++) {
            for(int j=0; j<COLUMNS; j++) {
                System.out.printf("%2d ",finalArray[i][j]);
            }
            System.out.println();
        }
    }

    private static void moveColumnUp(int matrix[][], int numberOfRows, int columnIndex) {
        int firstRowElement = matrix[0][columnIndex]; // temporarily store first row so it can be placed last
        for(int i=0; i<numberOfRows-1; i++) {
            matrix[i][columnIndex] = matrix[i+1][columnIndex];
        }
        matrix[numberOfRows-1][columnIndex] = firstRowElement;
    }
}
英文:

If I understood your question correctly, you're looking for something like this:

import java.util.stream.IntStream;
public class MultiDimensionalArrayDemo {
public static void main(String[] args) {
final int ROWS = 3;
final int COLUMNS = 10;
int array1 [] = {  0,  1,  2,  3,  4,  5,  6,  7,  8,  9};
int array2 [] = { 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
int array3 [] = { 20, 21, 22, 23, 24, 25, 26, 27, 28, 29};
int finalArray[][] =  new int[ROWS][];
finalArray[0] = array1;
finalArray[1] = array2;
finalArray[2] = array3;
System.out.println(&quot;Initial bi-dimensional array:&quot;);
for(int i=0; i&lt;ROWS; i++) {
for(int j=0; j&lt;COLUMNS; j++) {
System.out.printf(&quot;%2d &quot;,finalArray[i][j]);
}
System.out.println();
}
for(int i=0; i&lt;ROWS-1; i++) {
for(int j=0; j&lt;COLUMNS; j++) {
if(finalArray[i][j]==0) {
moveColumnUp(IntStream.range(i, ROWS).mapToObj(obj -&gt; finalArray[obj]).toArray(int[][]::new),ROWS-i,j);
}
}
}
System.out.println(&quot;After transformation:&quot;);
for(int i=0; i&lt;ROWS; i++) {
for(int j=0; j&lt;COLUMNS; j++) {
System.out.printf(&quot;%2d &quot;,finalArray[i][j]);
}
System.out.println();
}
}
private static void moveColumnUp(int matrix [][], int numberOfRows, int columnIndex) {
int firstRowElement = matrix[0][columnIndex]; // temporarily store first row so it can be placed last
for(int i=0; i&lt;numberOfRows-1; i++) {
matrix[i][columnIndex] = matrix[i+1][columnIndex];
}
matrix[numberOfRows-1][columnIndex] = firstRowElement;
}
}

huangapple
  • 本文由 发表于 2020年9月10日 17:56:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63827183.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定