92.9k views
5 votes
How to iterate through int[][] numarr={{1,2,3},{4,5,6,7},{8,9}};

1 Answer

2 votes

Final answer:

To iterate through the two-dimensional array int[][] numarr={{1,2,3},{4,5,6,7},{8,9}}, use nested for-loops. The outer loop goes over each sub-array, and the inner loop travels through each element of the sub-array.

Step-by-step explanation:

To iterate through a two-dimensional array in Java, such as int[][] numarr={{1,2,3},{4,5,6,7},{8,9}}, you'll need to use nested loops. The outer loop iterates over each sub-array (or 'row'), and the inner loop iterates over the elements of each sub-array (or 'column'). Here is an example of how you could iterate through numarr:

for (int i = 0; i < numarr.length; i++) {
for (int j = 0; j < numarr[i].length; j++) {
System.out.print(numarr[i][j] + " ");
}
System.out.println();
}

This code will go through each sub-array and print out all the elements, resulting in the numbers being printed row by row. The use of numarr.length allows you to iterate over each 'row', and numarr[i].length allows you to iterate over each 'column' of a given 'row'.

User Irwin
by
7.2k points