2d array gleicher index java

A multidimensional array is simply an array of arrays. You can look it as a single container that stores multiple containers.

In this article, we'll talk two dimensional arrays in Java. You'll see the syntax for creating one, and how to add and access items in a two dimensional array.

How to Declare a Two Dimensional Array in Java

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 Java

We 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]);
// 1

In 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.

So to access an item from the first array, we assigned 0 to the first square bracket. Since we were trying to access the first item in the array, we used its index which is zero:

int[][] oddNumbers = { {1, 3, 5, 7}, {9, 11, 13, 15} };
1.

Let's break it down even further.

Here's the code to access items:

int[][] oddNumbers = { {1, 3, 5, 7}, {9, 11, 13, 15} };
2

I've put question marks in both square brackets – we'll fill them in as we progress.

So let's say we want to access an item in the second array which is denoted using 1, our code will look like this:

int[][] oddNumbers = { {1, 3, 5, 7}, {9, 11, 13, 15} };
3.

Now that we're in the second array ({9, 11, 13, 15}) let's try to access an item in it. Just like regular arrays, each items has an index starting from zero.

So to access

int[][] oddNumbers = { {1, 3, 5, 7}, {9, 11, 13, 15} };
5 which is the third item, we pass its index number to the second square bracket:
int[][] oddNumbers = { {1, 3, 5, 7}, {9, 11, 13, 15} };
6.

In the next section, we'll start with a fresh example.

How to Access Items in a Two Dimensional Array in Java Example

int[][] oddNumbers = { {1, 3, 5, 7}, {9, 11, 13, 15}, {17, 19, 21, 23} };

The objective here is to access 21 in the third array. Our access code still has question marks:

int[][] oddNumbers = { {1, 3, 5, 7}, {9, 11, 13, 15} };
2.

We'll start by giving the first question mark a value which points to the particular array to access.

Array 0 => {1, 3, 5, 7}
Array 1 => {9, 11, 13, 15}
Array 2 =>

int[][] oddNumbers = { {1, 3, 5, 7}, {9, 11, 13, 15} };

System.out.println(oddNumbers[0][0]);
// 1
0

The number we're looking for is in the third array with an array index of 2. So we've found the value for the first square bracket:

int[][] oddNumbers = { {1, 3, 5, 7}, {9, 11, 13, 15} };

System.out.println(oddNumbers[0][0]);
// 1
1

The second square bracket's value will point to the actual item to be accessed. To do that, we have to specify the index number of the item. Here are the indexes in that array:

17 => Index 0
19 => Index 1
21 => Index 2
23 => Index 3

21 has an index of 2 so we can go on and add that to the second square bracket:

int[][] oddNumbers = { {1, 3, 5, 7}, {9, 11, 13, 15} };

System.out.println(oddNumbers[0][0]);
// 1
2. When you print that to the console, you'll get 21 printed out.

Here's what the code looks like:

int[][] oddNumbers = { {1, 3, 5, 7}, {9, 11, 13, 15}, {17, 19, 21, 23} };

System.out.println(oddNumbers[2][2]);
// 21

You can loop through all the items in a two dimensional array by using a nested loop. Here's an example:

int[][] oddNumbers = { {1, 3, 5, 7}, {9, 11, 13, 15}, {17, 19, 21, 23} };

for(int i = 0; i < oddNumbers.length; i++){
    for(int j = 0; j < oddNumbers[i].length; j++){
        System.out.println(oddNumbers[i][j]);
    }   
}

// 1
// 3
// 5
// 7
// 9
// 11
// 13
// 15
// 17
// 19
// 21
// 23

The code above prints out all the items in the oddNumbers array.

Summary

In this article, we talked about two dimensional arrays in Java.

We saw the syntax for creating two dimensional arrays. We also saw examples that showed how to access items stored in them.

Lastly, we saw how to loop through and print the items in a two dimensional array.

Happy coding!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT


2d array gleicher index java
Ihechikara Vincent Abba

This author's bio can be found in his articles!


If you read this far, tweet to the author to show them you care. Tweet a thanks

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

How to get the index of a 2D array in Java?

Accessing 2D Array Elements In Java, when accessing the element from a 2D array using arr[first][second] , the first index can be thought of as the desired row, and the second index is used for the desired column. Just like 1D arrays, 2D arrays are indexed starting at 0 .

How to print indices of 2D array in Java?

In the below example, we will show an example of how to print an array of integers in java. Code: public class Print2DArrayInJava { public static void main(String[] args) { //below is declaration and intialisation of a 2D array final int[][] matrx = { { 11, 22}, { 41, 52}, }; for (int r = 0; r < matrx.

How does indexing work in a 2D array Java?

Two-dimensional (2D) arrays are indexed by two subscripts, one for the row and one for the column. Each element in the 2D array must by the same type, either a primitive type or object type.

Does indexOf work on 2D array?

You cannot use indexOf to do complicated arrays (unless you serialize it making everything each coordinate into strings), you will need to use a for loop (or while) to search for that coordinate in that array assuming you know the format of the array (in this case it is 2d).