Java – Array of Arrays

You can define an array of arrays in Java. Outer array contains arrays elements. Inner arrays is just like a normal array of integers, or array of strings, etc.

Array of Arrays in Java
ADVERTISEMENT

Declare Array of Arrays

The syntax to declare an Array of Arrays in Java is

datatype[][] arrayName;

The second set of square brackets declare that arrayName is an array of elements of type datatype[].

For example, int[][] numbers, declares that numbers is an array of elements that are of datatype int[].

Initialize Array of Arrays

To initialize an array of arrays, you can use new keyword with the size specified for the number of arrays inside the outer array.

datatype[][] arrayName = new datatype[size][];

In the array of arrays, you can have elements only of the specified datatype. Elements of no other datatype are allowed, just like in one dimensional arrays

For example, the following line of code

int[][] numbers = new int[3][];

specifies that numbers is an array of arrays that store integers. Also, numbers array is of size 3, meaning numbers array has three arrays inside it. The size of the inner arrays can be different from each other.

Another way of initializing an array of arrays is to assign a list of lists to the array.

datatype[][] arrayName = { {element, element, ..}, {element, element, ..}, {element, element, ..} };

For example,

int[][] numbers = { {2, 3, 7, 1}, {9, 8, 6, 7}, {1, 7, 6, 4, 8} };

Example 1 – Array of Arrays in Java – Using new keyword

In the following example, we will use new keyword and create an array of arrays in Java.

Java Program

public class ArrayExample {
	public static void main(String[] args) {
		int[][] arrays = new int[3][]; 
		
		arrays[0] = new int[] {5, 7, 6, 4};
		arrays[1] = new int[] {1, 3, 1, 8, 6};
		arrays[2] = new int[] {9, 2, 3, 5};		
		
		for(int[] arr: arrays) {
			for(int n: arr) {
				System.out.print(n+" ");
			}
			System.out.println();
		}
	}
}

Output

5 7 6 4 
1 3 1 8 6 
9 2 3 5

Example 2 – Array of Arrays in Java – Assign List of Arrays

In the following example, we will assign the list of arrays to array variable that stores array of arrays.

Java Program

public class ArrayExample {
	public static void main(String[] args) {
		int[] array1 = {5, 7, 6, 4};
		int[] array2 = {1, 3, 1, 8};
		int[] array3 = {9, 2, 3, 5};
		
		//array of arrays
		int[][] arrays = {array1, array2, array3};
		
		for(int[] arr: arrays) {
			for(int n: arr) {
				System.out.print(n+" ");
			}
			System.out.println();
		}
	}
}

Output

5 7 6 4 
1 3 1 8 
9 2 3 5

Traverse through Array of Arrays

You can access individual elements using index. In the following line of code

arrays[1][3]

we have first selected the second array (with index as 1) inside arrays, and then we selected the fourth element (with index as 3) in that array.

You can iterate over the elements using any looping technique like while loop, for loop or advanced for loop.

In previous examples, we used advanced for loop, to traverse through the array elements. Also, this is recommended way of traversing through the elements, when reading data from array of arrays, because we do not need to explicitly take care of the array bounds.

In the following program, we will use for loop, to traverse through elements.

Java Program

public class ArrayExample {
	public static void main(String[] args) {
		int[] array1 = {5, 7, 6, 4};
		int[] array2 = {1, 3, 1, 8};
		int[] array3 = {9, 2, 3, 5};
		
		//array of arrays
		int[][] arrays = {array1, array2, array3};
		
		for(int index_1 = 0; index_1 < arrays.length; index_1++) {
			for(int index_2 = 0; index_2 < arrays[index_1].length; index_2++) {
				System.out.print(arrays[index_1][index_2]+" ");
			}
			System.out.println();
		}
	}
}

Output

5 7 6 4 
1 3 1 8 
9 2 3 5

Conclusion

In this Java Tutorial, we learned how to create an array containing arrays in Java, with the help of example programs.