Laucha
To create a two dimensional array in Java, you have to specify the data type of items to be stored in the array, followed by two square brackets and the name of the array.
Here's what the syntax looks like:
data_type[][] array_name;Let's look at a code example.
int[][] oddNumbers = { {1, 3, 5, 7}, {9, 11, 13, 15} };Don't worry if you're yet to understand what's going on above. In the next section, you'll learn more about how two dimensional arrays work and how to access items stored in them.
How to Access Items in a Two Dimensional Array in JavaWe can access items in a two dimensional using two square brackets.
The first denotes the array from which we want to access the items while the second denotes the index of the item we want to access.
Let's simplify the explanation above with an example:
int[][] oddNumbers = { {1, 3, 5, 7}, {9, 11, 13, 15} };System.out.println(oddNumbers[0][0]);// 1In the example above, we have two arrays in the oddNumbers array – {1, 3, 5, 7} and {9, 11, 13, 15}.
The first array — {1, 3, 5, 7} — is denoted using 0.
The second array — {9, 11, 13, 15} — is denoted using 1.
First array is 0, second is 1, third is 2, and so on.
Admin