This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Master Exam Array question

 
AustinRay Lee
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone explain the answer to this problem, master exam says that answer C below "is wrong because C tries to assign a two-dimensional array where a one-dimensional array is expected." I can see that b is a two-dimensional array, but it looks like b2 is four-dimensional array. So I would expect the answer to say "C is wrong because C tries to assign a two-dimensional array where a four-dimensional array is expected." Or why is b2 considered a one-dimensional array when it has 4 []'s.
public class Test{
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:
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's start simple and build up ...

Whew. Looks like what you can put into any slot is enough dimensions to fill out the original declaration. In "a[1]" that means no more dimensions, only an int. In d[1][1][1] as in your question, only one more dimension.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic