Search⌘ K

Array Manipulation

Explore how to manipulate arrays in Java through nested loops for multidimensional arrays, using built-in methods like toString and contains, and handling arrays as method parameters and return values. This lesson helps you implement array operations effectively in programming.

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.

Java
//Multidimensional array using nested for loop
//Array Example Eleven
class 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.

The

...