简体   繁体   中英

How can I print matrices of different dimensions in a 3D arrangement?

I don't know if you understand me, but I made a 3D array, where each "matrix" has the same rows but different columns:

int nMatrix = 3, rows = 5;
String[][][] matrix = new String[nMatrix][][];
matrix[0] = new String[rows][r.nextInt(3) + 5];
matrix[1] = new String[rows][r.nextInt(3) + 5];
matrix[2] = new String[rows][r.nextInt(3) + 5];

I wish I could print this but at the same time:

// 5x5 matrix
[ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ]

// 5x7 matrix
[ ] [ ] [ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ] [ ] [ ]

// 5x6 matrix
[ ] [ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ] [ ]

Is it possible to do something like that?

Try this.

for (String[][] x : matrix) {
    for (String[] y : x)
        System.out.println(Arrays.toString(y));
    System.out.println();
}

output:

[null, null, null, null, null]
[null, null, null, null, null]
[null, null, null, null, null]
[null, null, null, null, null]
[null, null, null, null, null]

[null, null, null, null, null, null, null]
[null, null, null, null, null, null, null]
[null, null, null, null, null, null, null]
[null, null, null, null, null, null, null]
[null, null, null, null, null, null, null]

[null, null, null, null, null, null]
[null, null, null, null, null, null]
[null, null, null, null, null, null]
[null, null, null, null, null, null]
[null, null, null, null, null, null]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM