6 ways to declare and initialize a two-dimensional (2D) String and Integer Array in Java - Example Tutorial (original) (raw)

For a detailed description of an array and how to use it in your code, I suggest you go through a good data structure and algorithm course like Data Structures and Algorithms: Deep Dive Using Java on Udemy.

It will not only teach you about array but also other fundamental data structures like a linked list, binary tree, and hash table, which are very important for your day-to-day programming.

Now, let's see a couple of ways to declare a two-dimensional array in Java to understand the concept better.

6 ways to declare and initialize a 2D String and Integer Array in Java

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 a one-dimensional array. This is the key to writing an algorithm involving multi-dimensional arrays like looping through a 2D array, searching elements, etc.

Now, that you know what is a 2D or two-dimensional array in Java, let's see a couple of examples of how to create and initialize a 2D array. I have chosen both int and String arrays as they are the most common type of array you will find while coding.

1. Declare the 2D array with both dimensions

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];

2. Declaring a two-dimensional array with just one dimension

In Java, you can declare a 2D array with just one dimension but that has to be the first dimension. Leaving both dimension or first dimension while declaring an array is illegal in Java and throw a 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][];

Btw, if you are a beginner and not familiar with array syntax in Java or if you want to learn more about Java Programming language, I also suggest you go through a comprehensive Java course like The Complete Java MasterClass on Udemy. It's also the most up-to-date course in Java and always updated to cover the latest Java version.

How to declare and Initialize 2D or two dimensional int and String Array in Java

3. Position of the square bracket

You can also interchange square bracket position while declaring an array in Java as shown in the following code example:

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

The important thing to note about this code is that here quotes is not a two-dimensional array but just one dimension array. We have actually declared int[] only

Another thing to remember about this code is that if multiple variables are declared in the same line they would be the type of int[] which is one dimensional, not two dimensional like in the following example prices is a 2D array but abc is just a one-dimensional int array.

int[] prices[], abc;

Again, this is a tricky array concept in Java and that's why you will often find questions on this topic on various Java certifications. Btw, if you are preparing for Java certification then don't worry, all good exam simulators and exam software like Whizlabs and David Mayer's OCAJP dumps covers this topic very well.

4. Another Example with the position of the square bracket

It is also possible to put both square brackets after the variable name like 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[][];

If you are still confused then I suggest you take a look at Java Fundamentals Part 1 and Part 2 in Pluralsight, two of the in-depth Java courses on Pluralsight.

how to declare 2D String array in Java with example

5. 2D array with the variable column length

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];

6. Declaring and initializing a heterogeneous 2D array

Creating a heterogeneous 2D array in Java. In the heterogeneous array, each subarray is of a different type like in the following example, the 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 a lot of flexibility and power e.g. you can declare a 2D array without specifying the second dimension, you can declare a two-dimensional array where each subarray is of a different length, and you can even create a heterogeneous 2D or two-dimensional array in Java.

Related Data Structure and Algorithm Interview Questions from Javarevisited Blog

Thanks for reading this article so far. If you like this Java array tutorial and example then please share it with your friends and colleagues. If you have any questions or doubt then please let us know and I'll try to find an answer for you. As always suggestions, comments, innovative and better answers are most welcome.

P. S. - If you are looking for some Free Algorithms courses to improve your understanding of Data Structure and Algorithms, then you should also check the Easy to Advanced Data Structures course on Udemy. It's authored by a Google Software Engineer and Algorithm expert and it's completely free of cost.