Wednesday, February 10, 2016

6 example to declare two dimensional array in Java

Declaring a two-dimensional array is interesting in Java as Java programming language provides many ways to declare a 2D array and each may have some special things to learn about. For example, It's possible to create a two-dimensional array in Java without specifying the second dimension, sounds crazy right? but it's possible because two-dimensional array in Java is nothing but an array of array. You can even create a two-dimensional array where each subarray has different length or different type, also known as a heterogeneous array in Java. Some of the ways described here also apply how you to declare one-dimensional array e.g. changing the place of square brackets and it has implication on any other variables declared in the same line.

An array is a data structure which stores elements in the contiguous  memory location and allowed constant time access using indexes. For a detailed description of an array, you should read a good data structure and algorithm book e.g. Introduction to Algorithms By Thomas Cormen, Charles Leiserson, Ronald Rivest and Clifford Stein. Now, let's see a couple of ways to declare a two-dimensional array in Java to understand the concept better.

As far as Java is concerned, you should always remember that there is nothing like a two-dimensional array, in fact, it's just an array of one-dimensional array.  This is the key to writing any algorithm involving multi-dimensional array e.g. looping through a 2D array, searching elements etc.

6 ways to declare 2D array in Java

Our first example is about declaring a 2D array with both dimensions. Let's declare a  homogeneous two-dimensional array following is a two-dimensional int array with 3 rows and 2 columns and next is 2 dimensional String array with 4 rows and 3 columns
int[][] squares = new int[3][2];
String[][] cubes = new String[4][3];

2nd example - declaring two-dimensional array with just one dimension
You can declare a 2D array with just one dimensional but that has to be the first dimension. Leaving both dimension or first dimension while declaring array is illegal in Java and throw compile time error as shown below:

int[][] numbers = new int[][] ; // error - first dimension mandatory
int[][] primes = new int[][2] ; // error - first dimension mandatory

int[][] multipliers = new int[10][]; // ok, second dimension is optional
String[][] names = new String[5][];

Our 3rd example - you can interchange square bracket position

int[] quotes[] = new int[2][2]; // ok

but above quote is declaring int[] only e.g. if multiple variables are declared in the same line they would be the type of int[] which is one dimensional, not two dimensional e.g. in following example prices is a 2D array but abc is just a one-dimensional int array.

int[] prices[], abc;

4th example - it is also possible to put both square brackets after variable name e.g. in following declaration number is int variable, a is one-dimensional int array and b is a two-dimensional int array.

int number, a[], b[][];

5th example - creating a 2D array where each array is of different length for example, in following code ids contain three one-dimensional int array each of different length i.e. 10, 20 and 30

int[][] ids = new int[3][];
ids[0] = new int[10];
ids[1] = new int[20];
ids[2] = new int[30];

6th example - creating a heterogeneous 2D array in Java. In the heterogeneous array, each subarray is of a different type. e.g. in the following example, items subarray is of Integer, String and Float type.

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

Here is a summary of different ways of declaring a two-dimensional array in Java:

6 ways to declare a two dimensional array in Java

That's all about 6 different ways to declare a two-dimensional array in Java. You can see 2D array offers lot of flexibility and power e.g. you can declare 2D array without specifying the second dimension, you can declare two dimensional array where each subarray is of different length, and you can even create a heterogeneous 2D array in Java.

Further Learning
Data Structures and Algorithms: Deep Dive Using Java
Algorithms and Data Structures - Part 1 and 2
Data Structures in Java 9 by Heinz Kabutz

Other Articles you may like
  • Introduction to Algorithms by Thomas Cormen (see here)
  • Top 30 Array-based Coding interview questions (list)
  • How to create and initialize Anonymous array in Java (answer)
  • How to find the missing number in an array? (answer)
  • How to compare two arrays in Java? (answer)
  • Data Structures and Algorithm Analysis in Java (see here)


Aakash Verma said...

If you execute those 4 lines of declaration in 6th example per se, it would definitely be a runtime error. To really induce dynamic binding as you want to with different wrapper types, you should actually go for "Objects[] items[]=new Objects[][];" and not "new String[][]".

Javin Paul said...

@Aakash, Yes, you are right, for storing objects, it has to be Objects[] items = new Object[][];

Post a Comment