In array, we can initialise something like this:
int[] arr = {1, 2, 3, 4, 5};
But for two dimensional array, why the below is not correct?
public class Main { public static void main(String[] args) { int[][] arr = new int[3][]; arr[0] = new int[]{1,2,3}; //This is the correct way arr[0] = {1,2,3}; // ->Why this is not right? } }
Asked
Active
Viewed 162 times
0

Reacher
- 356
- 4
- 16
-
2are you looking for an answer other than "because of the java language specification" ? – Patrick Parker Jul 01 '18 at 11:24
-
check this answer https://stackoverflow.com/questions/12231453/syntax-for-creating-a-two-dimensional-array – bembas Jul 01 '18 at 11:28
3 Answers
1
Yes, you are correct the second way of creating a 2D array will not work because when the compiler will compile the code because it couldn't decide its type.

Asheesh Sahu
- 342
- 2
- 12
-
thank you for your answer Asheesh, but when the first line of code execute: `int[][] arr = new int[3][]` does it indicate that all elements within would be all `int` type? – Reacher Jul 01 '18 at 11:36
-
1@Ruizhi arrays in java are reference types, so when you declare 2d array like `new int[3][]` this means that it will contain an array with 3 null references for another int arrays `[null, null, null]` – Arashigor Jul 01 '18 at 11:43
-
yes because when you write int[ ][ ] arr this means that you are going to create an Integer array. and new int[3][ ] would allocate memory at runtime . – Asheesh Sahu Jul 01 '18 at 11:44
-
@Asheesh Sahu I know Arrays are Object in java and memory is allocated to Objects only by using new keyword. but why for 1D array, it is allowed to use short hand like: `int[] arr = {1 , 2 , 3 }`, but not allowed in 2D array ? – Reacher Jul 01 '18 at 11:47
-
-
@Arashigor thank you, I still want to ask: null references to another `int` type, we can use `new` keyword to allocate memory at runtime, but why for 1D array, it is allowed to use short hand like: int[] arr = {1 , 2 , 3 }, and not allowed in 2D array ? – Reacher Jul 01 '18 at 12:01
-
@Ruizhi The int[] arr is just the reference to the array of integer. If you create an array with 10 integers, it is the same – an array is allocated and a reference is returned. While, Actually, we can only have the one-dimensional array in Java. 2D arrays are basically the just one-dimensional array of one-dimensional array. So to address that other next array we have to tell the compiler about that. – Asheesh Sahu Jul 01 '18 at 12:03
0
You can only use this syntactic sugar during variable initialization time.
Meaning that Java Language Specification allows only
arr[0] = new int[]{1, 2, 3};
or int[][] arr = {{1,2,3},{1,2,3},{1,2,3}};
.
Difference is that the first variant is a variable assignment and the second one is variable initialization.

Arashigor
- 171
- 3
- 12
-
thanks, but why I can use array int[] arr ={1,2,3}; I can't use it in 2D array initialisation ? – Reacher Jul 01 '18 at 11:28
-
You can initialize inline 2D array this way `int[][] arr = {{1,2,3},{1,2,3},{1,2,3}};` – Arashigor Jul 01 '18 at 11:30
-
-
0
The space for the second dimension would be allocate only with the code new int [ ] { 1, 2, 3}
. Before that the compiler doesn't know what the type of the array items and what the dimension is.

sven0379
- 11
- 4
-
thank you for your answer, but when the `int[][] arr = new int[3][];` execute, does it tell the compiler that all the elements would be `int` type, and it would be 2 dimension – Reacher Jul 01 '18 at 11:34