Two-Dimensional Arrays in Java
In Java, a two-dimensional array is an array of arrays. It allows you to store data in a grid-like structure with rows and columns. Each element in a two-dimensional array is accessed using two indices: one for the row and one for the column.
To declare and create a two-dimensional array in Java, you can use the following syntax:
data_type[][] array_name = new data_type[row_length][column_length];
For example, to create a two-dimensional array of integers with 3 rows and 4 columns, you can use the following code:
int[][] grid = new int[3][4];
You can also initialize a two-dimensional array with specific values using nested array initializers:
int[][] grid = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Here's a memory diagram representing the two-dimensional array "grid" with 3 rows and 4 columns:
1
2
3
0
4
5
6
0
7
8
9
0
Tags
Core Java tutorials