Search⌘ K
AI Features

Multi-dimensional Arrays

Explore the concept of multi-dimensional arrays in Java, including how to structure arrays of arrays and access individual elements with multiple indices. Understand practical examples like matrices and implement programs to calculate sums of rows and columns. This lesson helps you develop skills to work with complex array structures and apply them in real-world programming tasks.

What is a multi-dimensional array?

In Java, a multi-dimensional array is an array that contains other arrays as its values or members. The container array is termed an outer array, and the member array is termed an inner array. An array is said to be a multi-dimensional array if it has another array as its members.

Can you think of some instances where we might have been using a structure that is the same as a multi-dimensional array? One such example is a crossword puzzle!

Individual values in multi-dimensional arrays

The individual values in a multi-dimensional array are accessed through the multiple index numbers used. The following is an example of the two-dimensional array:

array1[row number][column number];

Let’s take the example of the following array:

int[][] array1 = {{10, 20, 30},
                   {16, 18, 20}};
  • The array1 is a two-dimensional array.
  • The value 20 can be accessed as array1 [1][2].
  • The number of indexes depends on the level of dimensions.

The general concepts of two-dimensional arrays, or n-dimensional arrays are implemented as follows in Java. The following program illustrates the structure of a two-dimensional array:

Java
class Test
{
public static void main(String args[])
{
int[][] array = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; //declaring a 2-dimensional array
for (int i = 0; i < 3; i++) // printing a 2-dimensional array
{
for (int j = 0; j < 3; j++)
{
System.out.print(array[i][j] + " ");
}
System.out.println(" ");
}
}
}

The following illustration shows the structure of the array above:

Here’s another example of the structure of a two-dimensional array:

Java
class Test
{
public static void main(String args[])
{
int[][] array = {{1, 2}, {4, 5}, {7, 8}}; //declaring a 2-dimensional array
for (int i = 0; i < 3; i++) // printing a 2-dimensional array
{
for (int j = 0; j < 2; j++)
{
System.out.print(array[i][j] + " ");
}
System.out.println(" ");
}
}
}

The following illustration shows the structure of the array above:

Here’s another example of a two-dimensional array:

Java
class Test
{
public static void main(String args[])
{
char[][] array = {{'A', 'B'}, {'C', 'D'}, {'E', 'F'}}; //declaring a 2-dimensional array
for (int i = 0; i < 3; i++) // printing a 2-dimensional array
{
for (int j = 0; j < 2; j++)
{
System.out.print(array[i][j] + " ");
}
System.out.println(" ");
}
}
}

The following illustration shows the structure of the above array.

A mathematical matrix is implemented as a two-dimensional array in Java, as shown in the following example:

matrix A=[123456789101112]\text{matrix} \ A= \begin{bmatrix} 1 & 2 & 3 & 4 \\ 5 & 6 & 7 & 8 \\ 9 & 10 & 11 & 12 \\ \end{bmatrix}

Java
class Test
{
public static void main(String args[])
{
int[][] matrixA = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
System.out.println("Matrix A");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
System.out.print(matrixA[i][j] + "\t");
}
System.out.println(" ");
}
}
}

The variable matrixA is an array of three elements, and each element is itself an array of four elements.

The following code demonstrates a way to write values in the forms of rows and columns:

Java
class Test
{
public static void main(String args[])
{
int[][] matrixB = {{10, 12, 13, 14},
{15, 16, 17, 18},
{19, 20, 21, 22}};
System.out.println("Matrix B");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
System.out.print(matrixB[i][j] + "\t");
}
System.out.println(" ");
}
}
}

The result of the second program is exactly the same as the first program. Inside the computer’s memory, both variables (matrixA and matrixB) are stored in the same way—as linear sequences. Now, let’s say we want to add these two matrixes and store them into another matrix (matrixC).

Java
class Test
{
public static void main(String args[])
{
int[][] matrixA = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}}; //declaring a matrixA
int[][] matrixB = {{10, 12, 13, 14},
{15, 16, 17, 18},
{19, 20, 21, 22}}; //declaring a matrixB
int[][] matrixC = {{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}}; //declaring an empty matrixC
System.out.println("Matrix A");
for (int i = 0; i < 3; i++) // Printing values of matrixA
{
for (int j = 0; j < 4; j++)
System.out.print(matrixA[i][j] + "\t");
System.out.println(" ");
}
System.out.println("Matrix B");
for (int i = 0; i < 3; i++) // Printing values of matrixB
{
for (int j = 0; j < 4; j++)
System.out.print(matrixB[i][j] + "\t");
System.out.println(" ");
}
for (int i = 0; i < 3; i++) // adding two matrixes
{
for (int j = 0; j < 4; j++)
matrixC[i][j] = matrixA[i][j] + matrixB[i][j];
}
System.out.println("Matrix C");
for (int i = 0; i < 3; i++) // Printing values of matrixC
{
for (int j = 0; j < 4; j++)
System.out.print(matrixC[i][j] + "\t");
System.out.println(" ");
}
}
}

The resulting matrix isn’t a sum of values of operands. Rather, it’s a matrix that has six members (three from matrixA and three from matrixB). We need to access the individual integer values in each matrix to produce their sum.

Practice multi-dimensional arrays

Here are a few example programs to practice using multi-dimensional arrays in Java. By clicking the “Show Solution” button, you’ll find a program that solves the respective problem. You can copy and paste the given solution into the code widget to make sure the output of your solution matches the given solution. There can be several ways of writing correct solutions in programming.

Calculate the sum of each row in a matrix

Write a program to show the sum of each row of the matrix. The matrix must have two rows and three columns.

Sample input 1

{{10, 20, 30},
 {40, 50, 60}}

Sample output 1

Matrix: 
10   20   30
40   50   60
Sum of Row1: 60
Sum of Row2: 150

Sample input 2

{{1, 2, 3},
 {4, 5, 6}

Sample output 2

Matrix: 
1   2   3
4   5   6
Sum of Row1: 6
Sum of Row2: 15
Java
class Test
{
public static void main(String args[])
{
// Write your code here
}
}

Calculate the sum of each column in a matrix

Write a program to show a matrix in the form of rows and columns, then show the sum of each column. The matrix must have five rows and ten columns.

Sample input

{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
 {10, 11, 12, 13, 14, 15, 16, 17, 18, 19},
 {20, 21, 22, 23, 24, 25, 26, 27, 28, 29},
 {30, 31, 32, 33, 34, 35, 36, 37, 38, 39},
 {40, 41, 42, 43, 44, 45, 46, 47, 48, 49}}

Sample output

Displaying in the matrix form:

 0	1	2	3	4	5	6	7	8	9	
10	11	12	13	14	15	16	17	18	19	
20	21	22	23	24	25	26	27	28	29	
30	31	32	33	34	35	36	37	38	39	
40	41	42	43	44	45	46	47	48	49	

Column sums are: 100 105 110 115 120 125 130 135 140 145
Java
class Test
{
public static void main( String args[] )
{
// Write your code here
}
}