Array Manipulation
Learn to manipulate multidimensional arrays.
We'll cover the following...
Nested loops for a 2D array
An important factor we should always keep in mind is that according to the depth of the multidimensional array, we can always use the nested for
loop. Consider the following example, where we have 2D arrays.
Based on its dimension, we use one nested for
loop.
Press + to interact
//Multidimensional array using nested for loop//Array Example Elevenclass Main{public static void main(String[] args){int[][] myArray = {{1, 2, 3},{4, 5, 6},{7, 8, 9}};for (int i = 0; i < 3; i++){System.out.print(i + " => ");for(int j=0;j<3;j++){System.out.print(myArray[i][j] + " ");}System.out.println();}}}
We get the following output when we run the code above:
0 => 1 2 3
1 => 4 5 6
2 => 7 8 9
In the above Java code:
Line 14: This line of code traverses and prints a myArray[i][j]
named 2D array using the indexes i
and j
.