# Java Array Tutorials
# Java Array Programs
➤ Find Length of Array
➤ Different ways to Print Array
➤ Sum of Array Elements
➤ Average of Array Elements
➤ Sum of Two Arrays Elements
➤ Compare Two Arrays in Java
➤ 2nd Largest Number in Array
➤ How to Sort an Array in Java
➤ Reverse an Array in Java
➤ GCD of N Numbers in Java
➤ Linear Search in Java
➤ Binary Search in Java
➤ Copy Array in Java
➤ Merge 2 Arrays in Java
➤ Merge two sorted Arrays
➤ Largest Number in Array
➤ Smallest Number in Array
➤ Remove Duplicates
➤ Insert at Specific Position
➤ Add Element to Array
➤ Remove Element From Array
➤ Count Repeated Elements
➤ More Array Programs
Java Matrix Programs
➤ Matrix Tutorial in Java
➤ Print 2D Array in Java
➤ Print a 3×3 Matrix
➤ Sum of Matrix Elements
➤ Sum of Diagonal Elements
➤ Row Sum – Column Sum
➤ Matrix Addition in Java
➤ Matrix Subtraction in Java
➤ Transpose of a Matrix in Java
➤ Matrix Multiplication in Java
➤ Menu-driven Matrix Operations
Traverse 2d Array Java | A 2d array is a multidimensional array. It can be also said that it is an array of arrays. We store the elements in the multidimensional array in rows and columns in the form of a matrix. Example for 2d array:-
int array[ ][ ] = {{1,2,3},{3,4,5},{6,7,8}};
This initializes the array directly, to take elements from the user we can declare it as follows:- int arr[ ][ ] = new int[ ][ ];
To access the array elements we need to use a loop. Let us demonstrate the iterating through a 2d array Java.
Iterating Through a 2d Array Java
public class Main {
public static void main(String[] args) {
int[][] elements =
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
System.out.println("The array elements are :");
for (int i = 0; i < elements.length; i++) {
for (int j = 0; j < elements[i].length; j++) {
System.out.print(elements[i][j] + "\t");
}
System.out.println("");
}
}
}
Output:-
The array elements are :
1 2 3
4 5 6
7 8 9
The Explanation of the above code of traversing 2d array Java goes as follows:- Here we have used the array of integers and initialized it directly. By using the for loops we print the array elements.
As this is a 2d array, therefore to access the elements we need two for loops. The loop will start from 0 to the length of the array. Also see:- How to find the length of the 2D array in Java
The elements.length gives the length of the row in the 2d array and elements[i].length gives the length of the ith column in the given 2d array. To access the elements we have to use elements[i][j].
Traverse 2d Array Java using Enhanced For Loop
There is a another way of using for loop we call this as for each loop. Let us see the demonstration of the for each loop.
public class Main {
public static void main(String[] args) {
int[][] elements =
{
{ 1, 1, 1 },
{ 2, 2, 2 },
{ 3, 3, 3 }
};
System.out.println("Using Enhanced for loop:");
for (int[] eachRow : elements) {
for (int j : eachRow) {
System.out.print(j + "\t");
}
System.out.println("");
}
}
}
Output:
Using Enhanced for loop:
1 1 1
2 2 2
3 3 3
Traversing 2d Array Java by Taking User Input
Now let us see how to traverse a 2d array in Java after taking input from the end-user.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[][] elements = new int[2][2];
System.out.println("Enter the elements in 2D array (2x2): ");
for (int i = 0; i < elements.length; i++) {
for (int j = 0; j < elements[i].length; j++) {
elements[i][j] = scan.nextInt();
}
}
System.out.println("The array Elements are :");
for (int i = 0; i < elements.length; i++) {
for (int j = 0; j < elements[i].length; j++) {
System.out.print(elements[i][j] + "\t");
}
System.out.println("");
}
scan.close();
}
}
Output:
Enter the elements in 2D array (2×2):
1 2
3 4
The array Elements are :
1 2
3 4
Also see:- Sum of matrix elements in Java, Sum of Diagonal Elements of Matrix
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!