Click here to Skip to main content
15,914,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why we can not assign value to array like this-
Java
int array[][]=new int[3][3];
array[][]={
           {1,2,3},
           {4,5,6},  //invalid
           {7,8,9}   
          };        

why we need to assign value separately like this
Java
array[0][0]=1;
array[0][1]=2;
array[0][2]=3;
.....
.....

why java only support assignation all value to array at the time of declaration of array like this only-
Java
int array[][]=new int[][]{
                          {1,2,3},
                          {4,5,6}, 
                          {7,8,9}   
                         }; 
Posted
Updated 29-Nov-15 3:52am
v2
Comments
Krunal Rohit 29-Nov-15 5:13am    
array[][]={1,2,3,4,5,6,7,8,9};
Here you are assigning value to only 1st dimension and the array is 2d.
Hence you need to go row-by-row.

-KR

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900