Two Dimensional Array in Java

Filed Under: Java

Today we will look into Two-dimensional array in java. An array is like a container that can hold a certain number of values.

Two Dimensional Array in Java

Let’s look at a few examples of defining java two-dimensional array or 2d array.

Java Two Dimensional Array of primitive type


int[][] arr = new int[2][3];
for (int i = 0; i < arr.length; i++) {
	for (int j = 0; j < arr[i].length; j++) {
		arr[i][j] = j;
		System.out.print(arr[i][j] + " ");
	}
	System.out.println("");
}

Java Two Dimensional Array of Objects


String[][] arrStr = new String[3][4];
for (int i = 0; i < arrStr.length; i++) {
	for (int j = 0; j < arrStr[i].length; j++) {
		arrStr[i][j] = "Str" + j;
		System.out.print(arrStr[i][j] + " ");
	}
	System.out.println("");
}

So we can define a two dimensional array of primitive data types as well as objects. If you look at the above examples, two dimensional array looks like a matrix, something like below image.

two dimensional array in java, java 2d array example

However, in Java, there is no concept of a two-dimensional array. A two-dimensional array in java is just an array of array. So below image correctly defines two-dimensional array structure in java.

two dimensional array java, java multidimensional array

Java multidimensional array example

Now if two-dimensional array in java is an array-of-arrays, then it should also support non-symmetric sizes as shown in below image.

java multidimensional array, multidimensional array in java

Well, it’s absolutely fine in java. Below is an example program that depicts above multidimensional array.


public class MultidimensionalArrayExample {

	public static void main(String[] args) {

		// creating and initializing two dimensional array with shortcut syntax
		int[][] arrInt = { { 1, 2 }, { 3, 4, 5 } };
		for (int i = 0; i < arrInt.length; i++) {
			for (int j = 0; j < arrInt[i].length; j++) {
				System.out.print(arrInt[i][j] + " ");
			}
			System.out.println("");
		}

		// showing multidimensional arrays initializing
		int[][] arrMulti = new int[2][]; // yes it's valid

		arrMulti[0] = new int[2];
		arrMulti[1] = new int[3];

		arrMulti[0][0] = 1;
		arrMulti[0][1] = 2;
		arrMulti[1][0] = 3;
		arrMulti[1][1] = 4;
		arrMulti[1][2] = 5;
		for (int i = 0; i < arrInt.length; i++) {
			for (int j = 0; j < arrInt[i].length; j++) {
				System.out.print(arrInt[i][j] + " ");
			}
			System.out.println("");
		}
	}
}

If we run the above program, it will produce the following output.


1 2 
3 4 5 
1 2 
3 4 5 

That’s all about two-dimensional array in java. In a similar way, we can define a multidimensional array in java too.

Reference: Java Array Oracle page

Comments

  1. Poojary says:

    Hi,
    char[2][2] are={{ ‘1’ , ‘2’ } , { ‘4’ , ‘6’ }};
    Is this the correct way to give value?
    If not can you tell me how to provide hard coded value to 2d char array..
    Thanks

  2. Duong says:

    Hi sir, I think multiple dimensional array same as int[][][].. arrMulti = new int[][][]..;

  3. LoRe says:

    Hi Pankaj,
    Your tutorials very informative with clear examples..
    If you add example snippet for accessing multi-dimensional arrays using for each loop, it will be more helpful to the beginners.

    Thanks.

  4. HIMANSU NAYAK says:

    Hi Pankaj,
    “String[][] arrMulti = new String[5][];”
    In this approach the columns has to be initialized before it is used.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages