0

Is that code for multidimensional array in Java really working for any suitable case within the arrays?

public class Main { public static void main(String[] args) { int[][] matrix = { {8, 1, 6}, {3, 5, 7}, {4, 9, 0}, }; //output the numbers for(int i = 0; i < matrix.length; i++) { for(int k = 0; k < matrix[i].length; k++) { System.out.println(matrix[i][k]); } } } }

2nd Apr 2022, 11:41 PM
tim szarfenord
3 Antworten
+ 2
What did you mean by "suitable case within the array"?
3rd Apr 2022, 2:21 AM
Ipang
+ 1
For example when the length of any of the arrays within the main array is longer or shorter than the others. The task was to create a code to print out every single element of the whole multidimensional array, but the solution for that task only used 'i < 3' & 'k < 3' for the conditions of the loops.
3rd Apr 2022, 9:46 AM
tim szarfenord
0
Sorry but apparently I still am failing to understand what your doubt was about. The one problem I see in that snippet is the use of println( ) for displaying the matrix's content. println( ) inserts a new line after printing the data, but I think that's not what we want to see, so use of print( ) is rather preferable. Here's some ways to printing matrices, hope it helps. You're welcome to confirm in case of any doubt. All these are available in Java tutorials BTW ... https://code.sololearn.com/cYAV7QNoKEs9/?ref=app
4th Apr 2022, 4:33 AM
Ipang