posted 24 years ago
Vivek , I reproduce your question below:
-----------------------------------------------------------------Is there any difference with these two statements?
int a[][] = new int[][]{{1,2,3} , {1,2,3}};
Or
int a[][] = {{1,2,3} , {1,2,3}};
---------------------------------------------------------------
I say YES, there is a difference.FASTEN YOUR SEAT BELT!!!
A.int a[][] = new int[][]{{1,2,3},{1,2,3}}; is called an array creation statement.
B.int a[][] ={{1,2,3},{1,2,3}}; is an array Initialiser.
The difference is that you can use A,(the array creation statement) anywhere in your code where you want to create a pre-declared array.But you can use B(the array initialiser) only at the same point you are declaring the array, otherwise it will be illegal and will not compile.Do you get it ? Oky, This is what I mean.
You can declare an array as int [][]a; and defer its creation to a later time. Then, at that opportune time you can legally write:
a = new int[][]{{1,2,3},{1,2,3}};//LEGAL, and it will work fine. However if you declared your array as above, without initializing, you CAN NOT later on say:
a ={{1,2,3},{1,2,3}};//ILLEGAL.You would have no choice now but to say a = new int[2][3]; or new int[2][] and initialise the elements one by one.
Do I make any Sense ?
Herbert.