# Multidimensional array in Java: Place an array in an array to create a table

Keywords: multidimensional array, one-dimensional array, array

In this article, we will look at the multidimensional array in Java. A multidimensional array consists of two or more dimensions.  We will, of course, see several different examples of how we can work with a multidimensional array in Java.

## What is a multidimensional array in Java?

A multidimensional array consists of two or more dimensions. We previously considered the one-dimensional array as a list that contained several elements that could have values. In the same way, we can view a multidimensional array in two dimensions as a matrix with rows and columns, like a grid or a chessboard.

If we were to illustrate a multidimensional array in two dimensions in the same way as we did for a one-dimensional array, we could imagine that it looks like:

Furthermore, we can imagine an array with three dimensions as a block, or a cube, and if we try to illustrate it, we get:

Before we continue and learn more about the multidimensional array, make sure you are familiar with the one-dimensional array in java.

## How a multidimensional array work in Java?

Similarly to the one-dimensional array, we can use, read and edit each element in the array using indexes. If we imagine a two-dimensional array (it is easiest to imagine) as a matrix arranged in rows and columns, we can read and edit elements in the same way as for a one-dimensional array by writing the array’s name followed by an index in square brackets.

If we illustrate what the index in a two-dimensional array looks like, we get an image according to figure 3 below, and to make it clearer, we have color-coded row index and column index.

The array we outlined in figure 3 consists of three columns and four rows, which gives a total of twelve elements.

## How to create a multidimensional array in Java?

A multidimensional array is created in a similar way as for a one-dimensional array, and it can also be described as that there are generally two approaches to creating a multidimensional array. We will go through syntax followed by a shorter example for both, and we have decided to call them approach one and approach two.

### Syntax: Declare two-dimensional array with approach one

If we start with syntax to declare a two-dimensional array that we think of as a table of rows and columns

```// Declare two-dimensional array with approach one
data type[][] name = new data type[number of elements(row)][number of elements (column)];```

### Syntax: Declare three-dimensional array with approach one

Furthermore, if we imagine a coordinate system with x-, y- and z-axis, we create a three-dimensional array by using.

```// Declare three-dimensional array with approach one
data type[][][]name = new data type[element x-axis][element y-axis][element z-axis];```

To conclude, let’s take the declaration step by step

• First, you specify the data type the array should consist of, followed by two square brackets [ ][ ] for a two-dimensional array, or three square brackets [ ][ ][ ] for a three-dimensional array
• Then you assign a name to the array followed by = new data type. The data type must be the same on both sides
• Finally, specify the number of elements that the array should contain within the square brackets for the rows and columns, followed by a semicolon

#### Example: Declare a two-dimensional array in Java using procedure one

Let’s take a short example to show what it looks like when using what we call approach one to create a two-dimensional array with 4 rows and 3 columns. If we want to edit or use an element in the array, we do it in the same way as for a one-dimensional array. We can start by inserting three values into our two-dimensional array twoAr.

```public class Example{
public static void main(String[] args) {

int [][] twoAr = new int ;

twoAr = 15;
twoAr = 7;
twoAr = 22;
}
}```

That will look like

Furthermore, if we then want to use the values we have put into our array, we can for example use

```public class Example{
public static void main(String[] args) {

int [][] twoAr = new int ;
twoAr = 15;
twoAr = 7;
twoAr = 22;

int sum = twoAr + twoAr;
int sumAll = twoAr + twoAr + twoAr;
int multi = twoAr * twoAr;

System.out.println(sum);
System.out.println(sumAll);
System.out.println(multi);
}
}```

Resulting in

```22
44
105```

### Syntax: Declare two-dimensional array with approach two

We can also create multidimensional arrays with what we call approach two, similarly as we saw for the one-dimensional arrays where we can directly assign values to the array element when declaring it.

```// Declare two-dimensional array with approach two
data type [][] name = {{ value, value, ... },
{ value, value, ... },
{ value, value, ... }};```

Note that in this case, it is curly brackets { } that are used to the right of the equals sign.

#### Example: Declare a two-dimensional array in Java using procedure two

Let’s look at a short example of how to create a two-dimensional array in Java with what we call approach two.

```public class Example{
public static void main(String[] args) {

int [][] twoArr2 = {{2, 3},
{34, 56},
{44, 654, 79},
{12, 9, 23, 44}};
}
}```

Moreover, if we then illustrate the array twoArr2

It is important to pay attention to that we have not created a 4 x 4 matrix; instead, the rows of our array are having a different number of columns. For example, if we would like to add an element to index  and type

```public class Example{
public static void main(String[] args) {

int [][] twoArr2 = {{2, 3},
{34, 56},
{44, 654, 79},
{12, 9, 23, 44}};

twoArr2 = 15;
}
}```

We will get java.lang.ArrayIndexOutOfBoundsException. In such cases, we need to create a new array to be able to add more indexes. In the next article, we will learn about ArrayList, which makes it possible to work with changing collections of data.

## Examples: Multidimensional array in Java

Let’s have a look at examples of how to use a two-dimensional and three-dimensional array in Java.

### Example 1: Search a two-dimensional array

If we want to loop through and search a two-dimensional array, we need to use two for-loops. Similarly, if we had a three-dimensional array, we would have needed three for-loops, so one for-loop per dimension. This can be a little tricky to understand at first, so let’s look at it with an easy and methodical example.

We use the same two-dimensional array, twoAr, from the previous example and insert a couple of values.

```public class Example{
public static void main(String[] args) {
int [][] twoAr = new int ;
twoAr = 15;
twoAr = 7;
twoAr = 46;
twoAr = 22;
twoAr = 33;
}
}```

Resulting in

Then we will use the two for-loops and the .length function to get the length of each row and each column.

```public class Example{
public static void main(String[] args) {
int [][] twoAr = new int ;
twoAr = 15;
twoAr = 7;
twoAr = 46;
twoAr = 22;
twoAr = 33;

for (int row = 0; row < twoAr.length; row++){
for (int column = 0; column < twoAr[row].length; column++){

}
}
}
}```

We use the first for-loop to go through each row. We will start at row 0 because the index for an array in Java always starts at 0.

`for (int row = 0; row < twoAr.length; row++){`

Then, we add the for-loop that checks each column in that row.

`for (int column = 0; column < twoAr[row].length; column++){`

What we are doing is going through the array row by row, in other words, we start at row index 0 and check all the elements there, then we jump down to row index 1, then continue like this. If we would print all elements in our array twoAr that don’t have the value 0, we see clearly which order the for loops goes through twoAr

```public class Example{
public static void main(String[] args) {
int [][] twoAr = new int ;
twoAr = 15;
twoAr = 7;
twoAr = 46;
twoAr = 22;
twoAr = 33;

for (int row = 0; row < twoAr.length; row++){
for (int column = 0; column < twoAr[row].length; column++){

// Evaluate that the value is not 0
if(twoAr[row][column] != 0){
System.out.println(twoAr[row][column]);
}
}
}
}
}```

Resulting in

```15
7
46
22
33```

Finally, what is usually a bit complicated to understand is the twoAr[row].length command, but what it does is that for each line, it takes the length of that line. In our case, all rows are the same length, but a two-dimensional array can have different length rows. Furthermore, note that the variable row is something we have created in the for loop

### Example 2: Find the largest value in a two-dimensional array

If we now would like to add a feature to our program. Say we want to find the largest value in the two-dimensional array from the previous example and print the value plus index where it was found.

```int [][] twoAr = new int ;
twoAr = 15;
twoAr = 7;
twoAr = 46;
twoAr = 22;
twoAr = 33;```

This is easily done by using two for loops and an if statement.

```int maxVar = twoAr;
int rowIndex = 0;
int columnIndex = 0;

for (int row = 0; row < twoAr.length; row++){
for (int column = 0; column < twoAr[row].length; column++){

if (twoAr[row][column] > maxVar){
maxVar = twoAr[row][column];
rowIndex = row;
columnIndex = column;
}
}
}```

Finally, we use System.out.println

```System.out.println("Value: " + maxVar + " at index " +
rowIndex + ":" + columnIndex);```

to show our result

`Value: 46 at index 2:2`

### Example 3: Search a three-dimensional array in Java

Let’s look at a third and final example where we will search through a three-dimensional array. To help us, we will use three for loops, and what we will do in this example is to show the value of each element with the associated index.

To begin with, we create a three-dimensional array with some randomly assigned values

```int[][][] threeArr = { { { 1, 2, 3 }, { 4, 5, 6 } },
{ { 10, 20, 30 }, { 40, 50, 60 } } };```

Then we initiate the three for-loops we use to go through all the elements in threeArr

```for (int i = 0; i < threeArr.length; i++)   {

for (int j = 0; j < threeArr[i].length; j++)  {

for (int k = 0; k < threeArr[i][j].length; k++) {

// Print the information
System.out.print("threeArr[" + i + "][" + j + "][" + k + "] = "
+ threeArr[i][j][k] + "\t");
}
// Used only for formating
System.out.println();
}
// Used only for formating
System.out.println();
}```

Finally, the result if we execute the code above will be

```threeArr = 1	threeArr = 2	threeArr = 3
threeArr = 4	threeArr = 5	threeArr = 6

threeArr = 10	threeArr = 20	threeArr = 30
threeArr = 40	threeArr = 50	threeArr = 60```

## Why use a multidimensional array in Java?

Basically, a multidimensional array is used to place an array in an array. It may sound complicated, but imagine that you want to save data in tabular format, for example, the test result for the students in a class. This means that we get more storage places to work with; therefore, usually described as an array of an array. In other words, a multidimensional array is used to store information in a matrix form; for example, a calendar or schedule cannot be realized as a one-dimensional array.

## Summary: Multidimensional array in Java

A multidimensional array consists of two or more dimensions. We can think of a two-dimensional array as a matrix or table and a three-dimensional array as a cube. A multidimensional array is usually described as a way to place an array in another array to group and work with data. Multidimensional arrays are created similarly as for a one-dimensional array, and it can also be said here that there are generally two approaches.

### Syntax: Declare two-dimensional array with approach one

```// Declare two-dimensional array with approach one
data type[][] name = new data type[number of elements(row)][number of elements (column)];```

### Syntax: Declare two-dimensional array with approach two

```// Declare two-dimensional array with approach two
data type [][] name = {{ value, value, ... },
{ value, value, ... },
{ value, value, ... }};```

## FAQ: Multidimensional array in Java

How does a multidimensional array work in Java?

In the same way, as for a one-dimensional array, we can use, read and edit each element in a multidimensional array using indexes. If we imagine a two-dimensional array as a matrix or a grid arranged in rows and columns, then we can, in the same way as for a one-dimensional array by writing the name of the array followed by an index in square brackets, read and edit elements.

How to loop through a multidimensional array in Java?

The easiest way is to use for loops. For example, if we were to search through a two-dimensional array, we would need to use two for-loops; similarly, if we were to have a three-dimensional array, we would need three for-loops. Thus, one for-loop per dimension could be said.

Does each row in a two-dimensional array have to be the same length?

No, it’s okay that they are of different lengths. We can easily create a two-dimensional array that contains different numbers of elements per row.