This week's book giveaway is in the Kotlin forum.We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# array assignment

Ranch Hand
Posts: 44
how this is true int [] [] arr=new int[3] [];

why this is error int [] [] arr=new int[] [3];

Prabhat Gupta
Ranch Hand
Posts: 135
-------------------------------------------
how this is true int [] [] arr=new int[3] [];

why this is error int [] [] arr=new int[] [3];
---------------------------------------------

Let us compara the intialization of 2-D array with 1-D array.
int [] num=new int[3];
it means num is an array reference that points to 3 integer variable.
int [] num={1,2,3}
it means num is an array reference that points to three integer which values are 1,2,3.

Here we notice that we must specify the number of reference it is pointing to.

Similarly for
int [] [] arr=new int[3] []; means arr is reference of two dimensional array
that ponits to 3 reference of 1-D array. also we can specify those 1-D array as arr[0]=new int[3]..arr[1]=new int[6]..arr[2]=new int[9]

as i mentioned earlier that we must provide the number of reference it would point.

but in the case of
int [] [] arr=new int[] [3]; we are not specifying that how many 1-D array will be pointed by 2-D array arr although we are specifying that each 1-D array reference will point to 3 int variable.

So,moral of story is we must specify the number of reference will be pointed by array reference variable.

if i m wrong any where please, correct me.