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
array1is a two-dimensional array. - The value
20can be accessed asarray1 [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:
The following illustration shows the structure of the array above:
Here’s another example of the structure of a two-dimensional array:
The following illustration shows the structure of the array above:
Here’s another example of a two-dimensional array:
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:
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:
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).
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
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