英文:
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("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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论