Friday, July 12, 2024

How to declare and initialize two dimensional Array in Java? Example Tutorial

There are many ways you can declare a two-dimensional integer array in Java. Since 2D array in Java is nothing but an array of array, you can even declare array with just one dimension, the first one; second dimension is optional. You can even declare a 2 dimensional array where each sub array has different lengths. You can also initialize the array at the time of declaration or on some later time by using nested for loop. So there is lots of flexibility. In this article, you will learn common Java idioms to declare and initialize two dimensional arrays with different lengths. 

How to declare a Two dimensional array in Java?

Following are the most common way to declare a 2D array in Java, where two angle brackets [][] are used to denote 2D array. following code create a 2D array with 5 rows and 5 columns. 

int[][] int2dArray = new int[5][5];

By default the matrix will be initialized with default value of int primitive i.e. 0, So each element of this 2D array will be zero. 

Similarly, you can declare a two dimensional array of String type :

String[][] String2dArray = new String[6][6];

This array has 6 rows and 6 columns and only hold String objects. 

you can even change the position of brackets but that will change the meaning slightly e.g. a int[][] means a type of 2 dimensional int array so following code will create three variables which can hold reference of 2D int array :

int[][] a, b, c;

if we do following :

int[] a[], b[], c;

then it will create two variables a, b which can hold 2 dimensional array but c will be just one dimensional array. So a very subtle but meaningful difference.



How to create two dimensional array with just one dimension?

It's Ok to declare a 2 dimensional array in Java with just one dimension, the first one because multi-dimensional array is nothing but array of array. So following is perfectly valid in Java :

int[][] table = new int[5][];

but this is invalid :

int[][] table = new int[][5]; // first dimension is mandatory

First code creates a 2D array with 5 sub array, length of 5 sub array can be same or different. 

If you want to create a 2D array where each sub-array is of different length then you can do following :
int[][] table = new int[3][];
table[0] = new int[5];
table[1] = new int[6];
table[2] = new int[7];

here first element is an integer array of length 5, second is an array of length 6 and third has length 7. 


How to create a heterogeneous 2 dimensional array in Java?

In Java, arrays are homogeneous, you cannot store an Integer on a String array, but if you create an object array you can store anything e.g. String, Integer or Long. Similarly, you can create a 2D heterogeneous array as shown below :
Object[][] matrix = new String[3][];

matrix[0] = new Integer[]{1, 3, 4,};
matrix[1] = new String[]{"A", "b"};
matrix[2] = new Float[]{1.0f, 2.0f}

You can see we have a heterogeneous array, which holds an integer, array and a float array. Technically its an object array though. 

How to declare and initializing 2D array in Java?

You can initialize the two dimensional array at the time of declaration or sometime later by using a for loop. If array is small then its better to initialize it at same line as shown below :
int[][] numbers = { {1, 2, 3}, {2, 4, 6}, {3, 6, 9} };

Now, one array quiz for you, answer in comments:

How to create 2 dimensional Array in Java? Example Tutorial


That's all about how to declare two dimensional array in Java. We have seen multiple ways to create a two dimensional array in Java like creating a 2 dimensional array with just one dimension, example of creating a homogeneous sand heterogeneous array as well creating and initializing the array at same time. We have also seen Java idioms to create different kinds of array variable and when to use the. 

2 comments :

Anonymous said...

Answer is 2 arrays

Anonymous said...

3

Post a Comment