• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about arrays....

 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 21015
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Henry Wong
author
Marshal
Pie
Posts: 21015
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic