These functions should work.
// First, cache your array dimensions so you don't have to
// access them during each iteration of your for loops.
int rowLength = array.length, // array width (# of columns)
colLength = array[0].length; // array height (# of rows)
// This is your function:
// Prints array elements row by row
var rowString = "";
for(int x = 0; x < rowLength; x++){ // x is the column's index
for(int y = 0; y < colLength; y++){ // y is the row's index
rowString += array[x][y];
} System.out.println(rowString)
}
// This is the one you want:
// Prints array elements column by column
var colString = "";
for(int y = 0; y < colLength; y++){ // y is the row's index
for(int x = 0; x < rowLength; x++){ // x is the column's index
colString += array[x][y];
} System.out.println(colString)
}
In the first block, the inner loop iterates over each item in the row before moving to the next column.
In the second block (the one you want), the inner loop iterates over all the columns before moving to the next row.
tl;dr:
Essentially, the for()
loops in both functions are switched. That's it.
I hope this helps you to understand the logic behind iterating over 2-dimensional arrays.
Also, this works whether you have a string[,] or string[][]