wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please hava a look at this question about array !!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Please hava a look at this question about array !!!" Watch "Please hava a look at this question about array !!!" New topic
Author

Please hava a look at this question about array !!!

kevin saber
Greenhorn

Joined: Mar 07, 2010
Posts: 21
ExamLab for sjcp 6.0 wrote:
A question from ExamLab for sjcp 6.0:
Which one is the valid statement?

A int[] it1 = new int[0]{};
B new int[2];
C int[] it2 = new int[][]{{1}}[0];
D int[][] it3 = new int[]{0}[0][0];
E int it5 = new int[2]{}[]


The answer is C.

Doesn't it covert from int[][][] to int[] ?
And about answer D, the explaination says it coverts from int[] to int[][]. Why doesn't it actually covert int[][][] to int[][] ?
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3019
    
  35

Please QuoteYourSources and please UseAMeaningfulSubjectLine


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

It does not convert int[] to int[][].

A 2 dimensional array is as array of arrays.



code is assigning a one dimensional array to one dimensional array. int[][]{{1}}[0] is creating a two dimensional array and initializing the first dimension only then the code is using the [0] operator to get the array at 0th location and array will be one dimension array. So it is legal to assign it to one dimension array.


int[][] it3 = new int[]{0}[0][0]; is not legal because [0][0] will give us the element at that location. That element will be of type int and we can not assign an int to an array.
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
In case of

A- It is creating an anonymous array. You cannot specify the dimension when you create an anonymous array. So statement A is invalid.
C- It is creating a two dimensional anonymous array and it is assigning the first array that is the 0 one to the one dimension array which
is perfectly legal. Because 2-D arrays are array or arrays.
E- has the same problem an anonymous array can't have dimensions mentioned.

Later is very well explained by harpreet.

Hope this helps,

Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
kevin saber
Greenhorn

Joined: Mar 07, 2010
Posts: 21
Devaka Cooray wrote:Please QuoteYourSources and please UseAMeaningfulSubjectLine


Sorry for that
kevin saber
Greenhorn

Joined: Mar 07, 2010
Posts: 21
Harpreet Singh janda wrote:It does not convert int[] to int[][].

A 2 dimensional array is as array of arrays.



code is assigning a one dimensional array to one dimensional array. int[][]{{1}}[0] is creating a two dimensional array and initializing the first dimension only then the code is using the [0] operator to get the array at 0th location and array will be one dimension array. So it is legal to assign it to one dimension array.


int[][] it3 = new int[]{0}[0][0]; is not legal because [0][0] will give us the element at that location. That element will be of type int and we can not assign an int to an array.


Thank you. I think i have caught it
kevin saber
Greenhorn

Joined: Mar 07, 2010
Posts: 21
Prithvi Sehgal wrote:In case of

A- It is creating an anonymous array. You cannot specify the dimension when you create an anonymous array. So statement A is invalid.
C- It is creating a two dimensional anonymous array and it is assigning the first array that is the 0 one to the one dimension array which
is perfectly legal. Because 2-D arrays are array or arrays.
E- has the same problem an anonymous array can't have dimensions mentioned.

Later is very well explained by harpreet.

Hope this helps,


Oh~~I see it. Thank you
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3019
    
  35

kevin saber wrote:Sorry for that

NoNeedToSaySorry
But, please QuoteYourSources now - it's mandatory. Questions without a proper source, will be deleted.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Please hava a look at this question about array !!!
 
Similar Threads
sjcp += 1;
SJCP 1.6 !!!
SJCP question
SJCP question .
To All who've cleared the SJCP