Two-Dimensional Arrays in Java
This is one of the articles from our Java Tutorial for Beginners.
Do you already know what arrays in Java are and how to work with them?
- If the answer is "No," then start by reading this article: "Java Arrays"
- If the answer is "Yes," then read the article below, which is about two-dimensional arrays in Java.
Since you already know what arrays are and how to work with them, it should be easy for you to:
- create a one-dimensional array
- assign values to it
- and display it in the console
Example:
1 2 3 4 5 6 7 8 9 10 |
class Test { public static void main(String[] args) { int[] array = {51,136, 387}; for (int i = 0; i < array.length; i++) { System.out.println(array[i]); } } } |
How Do We Create Two-Dimensional Arrays?
The most popular example of a two-dimensional array is probably the matrix. If you've forgotten what a matrix is, here's a reminder:
A matrix has rows and columns. Specific values stand at the intersections of these rows and columns.
Note: The index numbers of elements in arrays start from 0.
Number 1 is at the intersection of row 0 and column 0.
Number 2 is at the intersection of row 0 and column 1.
Number 3 is at the intersection of row 1 and column 0.
Number 4 is at the intersection of row 1 and column 1.
There are two ways to create two-dimensiona arrays:
1. In the code below, we created a two-dimensional array, but we didn't assign it any values.
1 2 3 4 5 6 |
class Test { public static void main(String[] args) { int[][] array = new int[2][2]; } } |
2. With this approach, we created a two-dimensional array and assigned it values at the same time.
1 2 3 4 5 6 |
class Test { public static void main(String[] args) { int[][] array = {{1,2}, {3,4}}; } } |
When you create an ordinary array, you use one pair of brackets. In a two-dimensional array, you use two pairs of brackets.
- In the first pair of brackets, you write the quantity of rows.
- In the second pair of brackets, you write the quantity of columns.
Example:
1 2 3 4 5 6 7 8 9 10 11 |
class Test { public static void main(String[] args) { int[][] array = new int[2][2]; array[0][0] = 1; array[0][1] = 2; array[1][0] = 3; array[1][1] = 4; } } |
And How Do We Display a Two-Dimesional Array In the Console?
The output of a two-dimensional array (with the help of the "for" loop) is very different from the output of an ordinary array with a "for" loop.
In order to display a value in each cell of a two-dimensional array, it's not enough to use a single "for" loop; instead, it's necessary to use two "for" loops, and one of them has to be located inside the other.
Why?
We mentioned earlier that a two-dimensional array consists of rows and columns, and each cell of such an array is at the intersection of a row and a column.
The first "for" loop checks each element of each row of a two-dimensional array (that contains come quantity of columns) to see if it matches the conditions.
The second "for" loop checks each element of each column in each row. By inserting one "for" loop inside another, you can assign values to elements of a two-dimensional array.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class Test { public static void main(String[] args) { int[][] array = new int[2][2]; array[0][0] = 1; array[0][1] = 2; array[1][0] = 3; array[1][1] = 4; for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { System.out.print(array[i][j] + "\t"); } System.out.println(); } } } |
If you run this code on your computer, you will see the following in the console:
1 2
3 4
You can find more articles in our Java Tutorial for Beginners.