This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes about arrays.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "about arrays...." Watch "about arrays...." New topic
Author

about arrays....

Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
This is from master exam....

class ArrTest
{
public static void main(String[] args)
{

short[][]b=new short[4][4];
short[][]big = new short[2][2];
short b3=8;
short b2[][][][]=new short[2][3][2][2];

// insert code here


A.b2[1][1]=big;

B.b[1][0]=b3;

C.b2[0][1][1]=b;

D.b2[0][2][1]=b[1][0];

E.b2[1][1][0][1]=b[1][0];

F.b2[1][1]=b;

answer is:A,B,E,F...

how to solve this type of problems related to arrays?

can anyone help me?
[ November 01, 2008: Message edited by: Ganeshkumar cheekati ]

SCJP5 and SCWCD1.5
Think Twice Act Wise...
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Ganeshkumar, when you copy a question from a book or mock exam, we require that you quote your sources. So, please tell us where you copied this from.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18492
    
  40


F.b2[1][1]=b;

answer is:A,B,E,F...

how to solve this type of problems related to arrays?


Well, let's look at one example... Choice F.



"b" is defined as a reference to an array of arrays of shorts.

"b2" is defined as a reference to an array of arrays of arrays of arrays of shorts.

b2[1][1] is dereferencing b2 array twice. Basically, it is dereferenced to the indexed one array (of the 4D array, which is a 3D array). And then, dereferenced again to the index one array (of the 3D array, which is a 2D array)... so b2[1][1] is an array of arrays of shorts.

Since, b and b2[1][1] is of the same type, the assignment is allowed.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18492
    
  40

Since, b and b2[1][1] is of the same type, the assignment is allowed.


And BTW, this is not a copy of the short elements. The array at b2[1][1] is now referring to the array referred to by b. So, there is now a 4 by 4 array at b2[1][1], whereas all the other arrays at b2[X][Y] are all 2 by 2 arrays (as defined during instantiation).

Henry
 
jQuery in Action, 2nd edition
 
subject: about arrays....
 
Similar Threads
Array reference assignments for multidimensional arrays
Array Reference Assignments
Multi-dim Array
Can you explain this with code...
Multi Dimensional Arrays