## Syntax for creating a two-dimensional array

### Question

Consider:

``````int[][] multD = new int[];
multD = new int;
``````

Is this how you create a two-dimensional array with 5 rows and 10 columns?

I saw this code online, but the syntax didn't make sense.

2015/07/09
1
443
7/9/2015 10:30:33 PM

Try the following:

``````int[][] multi = new int;
``````

... which is a short hand for something like this:

``````int[][] multi = new int[];
multi = new int;
multi = new int;
multi = new int;
multi = new int;
multi = new int;
``````

Note that every element will be initialized to the default value for `int`, `0`, so the above are also equivalent to:

``````int[][] multi = new int[][]{
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
``````
2013/05/21
823
5/21/2013 6:04:34 PM

We can declare a two dimensional array and directly store elements at the time of its declaration as:

``````int marks[][]={{50,60,55,67,70},{62,65,70,70,81},{72,66,77,80,69}};
``````

Here int represents integer type elements stored into the array and the array name is 'marks'. int is the datatype for all the elements represented inside the "{" and "}" braces because an array is a collection of elements having the same data type.

Coming back to our statement written above: each row of elements should be written inside the curly braces. The rows and the elements in each row should be separated by a commas.

Now observe the statement: you can get there are 3 rows and 5 columns, so the JVM creates 3 * 5 = 15 blocks of memory. These blocks can be individually referred ta as:

``````marks  marks  marks  marks  marks
marks  marks  marks  marks  marks
marks  marks  marks  marks  marks
``````

NOTE:
If you want to store n elements then the array index starts from zero and ends at n-1. Another way of creating a two dimensional array is by declaring the array first and then allotting memory for it by using new operator.

``````int marks[][];           // declare marks array
marks = new int;   // allocate memory for storing 15 elements
``````

By combining the above two we can write:

``````int marks[][] = new int;
``````
2013/08/24

You can create them just the way others have mentioned. One more point to add: You can even create a skewed two-dimensional array with each row, not necessarily having the same number of collumns, like this:

``````int array[][] = new int[];
array = new int;
array = new int;
array = new int;
``````
2015/07/09

The most common idiom to create a two-dimensional array with 5 rows and 10 columns is:

``````int[][] multD = new int;
``````

Alternatively, you could use the following, which is more similar to what you have, though you need to explicitly initialize each row:

``````int[][] multD = new int[];
for (int i = 0; i < 5; i++) {
multD[i] = new int;
}
``````
2012/09/01

It is also possible to declare it the following way. It's not good design, but it works.

``````int[] twoDimIntArray[] = new int;
``````
2015/07/09

Try:

``````int[][] multD = new int;
``````

Note that in your code only the first line of the 2D array is initialized to 0. Line 2 to 5 don't even exist. If you try to print them you'll get `null` for everyone of them.

2015/07/09