Java multidimensional arrays Tutorial

In this section we will learn what the multidimensional arrays are and how to use them in Java.

Note: Before reading this section we’re assuming you already familiar with arrays.

What Is a Multidimensional Array in Java?

A multidimensional array is an array that each of its elements is itself another array. And those sub-arrays can themselves have another list of arrays as their elements and so on.

public class Simple {
public static void main(String[] args) {
int [][] array = {
{1,2,3,4,5,6},
{7,8,9,0,1,2}
};
int second[][] = new int[10][10];
}
}

In this example the variable named array is a two dimensional array. This means the variable has two elements but each of these two elements is itself another array that has 6 integer elements.

The second array in this example is named second and it is another two dimensional array that has 10 elements and each of these elements is another array that can store 10 integer values.

Multidimensional Array in Java: Array of Array in Java

Alright, now that we have a general view of how a multi-dimensional array looks like, let’s get into the syntax of such type of arrays:

Data-type array-name[][][] = new Data-type [number-of-elements-in-first-array][ number-of-elements-in-sub-array][ number-of-elements-in-sub-sub-array]…;

There’s no difference between using data-type and array-name in single and multidimensional arrays.

[number-of-elements-in-first-array]: the first pair of bracket is used to set the number of the elements that this array will have.

Note: here we put the number of elements and we don’t care if those elements are themselves another array or not.

[ number-of-elements-in-sub-array]: the second pair of bracket is used to declared that each element of the last array is itself another array and the number of elements in that array will be declared in this second pair of bracket.

[ number-of-elements-in-sub-sub-array]: We can declare as many brackets [] as we want, and that means each element of the last array is itself another array and via this pair of brackets we’re declaring the size of each of these elements.

Note: On the left and right side of the assignment = operator, we should declare the same amount of brackets []. But on the left side brackets we don’t need to set the size.

Example: declaring multidimensional arrays in Java

int integerArray[][][] = new int [220][10][30];
double doubleArray[][] = new double [102][234];
long longArray[][][] = new long [100][200][300];

In this example, the integerArray variable is a three-dimensional array:

  1. This array can take 220 elements and each of these elements is
    itself another array of type integer.

  2. This sub-array can store 10 elements of type `int` but each of
    these elements is itself another array.

  3. The sub-sub-array can store 30 elements of type `int` and
    fortunately these elements are just simple integer values (No
    sub-array).

The second variable is doubleArray and this variable is a two-dimensional array:

  1. The `doubleArray` is an array that can store 102 elements and
    each of these elements is itself another array.

  2. This sub-array can store 234 elements of type `double` and each
    of these elements can be just a simple double value.

The third variable is longArray and this variable is a three-dimensional array:

  1. The `longArray` is an array that can store 100 elements and each
    of these elements is itself another array.

  2. This sub-array can store 200 elements and each of these elements
    is itself another array.

  3. This sub-sub-array is an array that can store 300 elements of
    type double.

Initialization of Multidimensional array in Java (Multidimensional Literal Array)

Although rarely needed, we can initialize a multidimensional array right where it’s being declared just like the way we did for a single dimensional array!

Note: for each sub-array we use a pair of braces and put its elements in those braces. Also each sub-array is separated from the other using a comma ,.

Example: initialization of multidimensional array in Java

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

In this example, both the first and the second element of the array are sub-arrays that can store 6 integer values.

Note: each sub-array can have a different number of elements in its body but the data type of all of them should be the same.

Access the Elements In multidimensional Array

In order to access elements in a multi-dimensional array, we use brackets just like the way we used it for single-array.

Note: the more brackets we use the deeper level we get.

Example: Accessing Elements of a Multidimensional Array in Java

For example, let’s say we have a two-dimensional array:

double doubleArray[12][24];

Now let’s say we want to access the third element of the first array list in the doubleArray:

doubleArray[0][2];

Or let’s say we want to access the 20th element of the second array list in the doubleArray:

doubleArray[1][19];

So the index that we set for the first bracket gives us the access to the elements of the first array and the second one gives us the access to the elements of the sub-array.

Java 2D Arrays: Java Two Dimensional Array

Creating an array within another array or basically an array that its elements are themselves arrays but the elements of these sub-arrays are just simple values (non-array) is called Two Dimensional Array (AKA 2D Array).

You’ve see this type of array in this section but we just didn’t officially introduce them.

Example: creating 2D arrays

class Main{
public static void main(String[] args) {
int age [][]= {
{80,90,93,50,99,76,100,88,99,30},
{50,96,97,50,91,74,59,83,93,38},
{84,91,97,54,91,75,10,85,96,36},
{60,81,90,54,56,89,78,98,78,98},
{95,45,98,62,85,78,98}
};
}
}

Java 3D Arrays: Java Three Dimensional Array

A Three Dimensional Array is an array that its elements are themselves arrays and also the elements of these sub-array are arrays as well! But the elements of the sub-sub-array are just simple values (non-array).

So basically a Three Dimensional array is like: Array→ sub-array → sub-sub-array → simple-elements

Example: creating 3D arrays

class Main{
public static void main(String[] args) {
int age [][][]= {
{
{
1,2,3,4,5,6
},
{
7,8,9,10
}
},
{
{
11,12,13,14,15
}
}
};
System.out.println(age[0][0][2]);
System.out.println(age[1][0][2]);
}
}
Output:
3
13

Java Print Multidimensional Array: How to Print a Multidimensional Array in Java

As you’ll soon see, using loops like for-each, for loop etc. we can iterate through the elements of an array the print those elements if needed.

Java Multidimensional Array and for loop

To loop through a multidimensional array, we need a for-loop for each dimension of the array.

For example, if we have a 2 dimensional array, there should be 2 loops (the second one goes inside the first loop) in order to loop through all the elements within the array and the sub-arrays (the elements of the first array).

Example: using for loop to iterate a multidimensional array

public class Simple {
public static void main(String[] args) {
int age [][]= {
{80,90,93,50,99,76,100,88,99,30},
{50,96,97,50,91,74,59,83,93,38},
{84,91,97,54,91,75,10,85,96,36},
{60,81,90,54,56,89,78,98,78,98},
{95,45,98,62,85,78,98,100,64,80}
};
int firsSize = age.length;
int secondSize = age[0].length;
for (int i = 0; i<firsSize; i++){
for (int b = 0 ; b<secondSize; b++){
System.out.print(age[i][b]+", ");
}
}
}
} Output:

80, 90, 93, 50, 99, 76, 100, 88, 99, 30, 50, 96, 97, 50, 91, 74, 59, 83, 93, 38, 84, 91, 97, 54, 91, 75, 10, 85, 96, 36, 60, 81, 90, 54, 56, 89, 78, 98, 78, 98, 95, 45, 98, 62, 85, 78, 98, 100, 64, 80,

As you can see the variable set in the first loop i is set as the index number of the first brackets [] and the second variable that is set in the second loop b is considered as the index number of the second brackets [] of the array

Example: using for-each loop to iterate a multidimensional array

public class Simple {
public static void main(String[] args) {
int age [][]= {
{80,90,93,50,99,76,100,88,99,30},
{50,96,97,50,91,74,59,83,93,38},
{84,91,97,54,91,75,10,85,96,36},
{60,81,90,54,56,89,78,98,78,98},
{95,45,98,62,85,78,98,100,64,80}
};
for (int [] array: age){
for (int a: array){
System.out.print(a+", ");
}
}
}
} Output:

80, 90, 93, 50, 99, 76, 100, 88, 99, 30, 50, 96, 97, 50, 91, 74, 59, 83, 93, 38, 84, 91, 97, 54, 91, 75, 10, 85, 96, 36, 60, 81, 90, 54, 56, 89, 78, 98, 78, 98, 95, 45, 98, 62, 85, 78, 98, 100, 64, 80,

In this example, we know that each element of the age array is itself another array.

So in the first for each loop, the variable that we set in the loop is a single dimensional array of type integer named array (Again this is because each element of the age variable is a single dimensional array and so the variable of the loop should match the type of elements of the array).

Now in the inner for each loop (the second one), we’ve set a variable named a that’ll get the elements of the array variable.

Jagged Array in Java

As mentioned before, a multidimensional array can have sub-arrays and each of those sub-arrays can have different number of elements (although all of them should be of the same type).

This is known as jagged Array.

Creating a jagged array using multidimensional array literals is as easy as just adding any number of elements to the sub-arrays as you’ve seen in the previous examples.

But when it comes to just declaring a jagged array, you need to declare the number of elements for each array before start to use it.

For example, if you have a 3D array, you need to at least set the number of elements that the top array (the first array) will have! But you don’t need to set the size of the sub-arrays at this time.

For example:

int [][][] arr = new int[10][][];

As you can see, the arr size is set to 10 which means it can take 10 elements and each element is a 2D array! But the size for these 2D arrays are undeclared which is fine at this stage!

Now before initializing the sub-arrays, we first need to declare them!

For example:

arr[0] = new int[20][10];

Here we’ve declared the first element of the arr array to a 2D array that has 20 element that themselves are arrays and each array has 10 non-array elements.

Now we can put values for those sub-arrays of the first element of the arr.

Basically, you should remember to first declare an array or its sub-array and then assign values as its elements.

Example: Jagged Array

class Main{
public static void main(String[] args) {
int [][][] arr = new int[3][][];
arr[0] = new int[20][10];
arr[1] = new int[3][2];
arr[2] = new int[4][10];
arr[0][10][1] = 10;
System.out.println(arr[0][10][1]);
}
}
Output:
10