• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

array question and confusion

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got so confused about this array question from KB book. Can someone please explain to me why B is correct and C is wrong ?? I really couldn't see why C is wrong.



A: b2[0][1] = b;
B: b[0][0]= b3;
C: b2[1][1][0]=b[0][0];
D: b2[1][2][0]=b;
E: b[0][1][0][0]=b[0][0];
F: b2[0][1]=big;
Suggested answer is A,B,E,F. B is the one that confused me the most. According to page 32. Wouldn't answer B contradicted with the explaination
below ?

int[][] books = new int [3][1];
int[] numbers = new int[6];
in aNumber = 7;
books[0] = aNumber; //NOT OK, expecting an int array instead of an int
books[0] = numbers; //OK, numbers is an int array.

Thx in advance,
Kay

( tags corrected and typos in code corrected)
[ December 07, 2004: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kay Liew:
I got so confused about this array question from KB book. Can someone please explain to me why B is correct and C is wrong ?? I really couldn't see why C is wrong.

<CODE>
public class Test{
public staic void main(String [] args){
btye [][] big = new byte [7][7];
byte [][] b = new new byte [2][1];
byte b3 = 5;
byte b2 = [][][][] = new byte [2][3][2][2];
}
}
</CODE>

A: b2[0][1] = b;
B: b[0][0]= b3;
C: b2[1][1][0]=b[0][0];
D: b2[1][2][0]=b;
E: b[0][1][0][0]=b[0][0];
F: b2[0][1]=big;
Suggested answer is A,B,E,F. B is the one that confused me the most. According to page 32. Wouldn't answer B contradicted with the explaination
below ?

int[][] books = new int [3][1];
int[] numbers = new int[6];
in aNumber = 7;
books[0] = aNumber; //NOT OK, expecting an int array instead of an int
books[0] = numbers; //OK, numbers is an int array.

Thx in advance,
Kay



Hi,...
According to me, B is indeed correct and C is indeed wrong.


B: b[0][0]= b3;


B is the normal initialization of the array (in this case) and is the easiest part to be notified as correct answer!!!

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


At the time you answer the question, did you regard answer 'A' as the correct answer??? I think if you put 'A' as the correct answer, then you should be able to know why C is incorrect, right?

C may only be correct, such as b2[1][1][0][0] = b[0][0];

E: b[0][1][0][0]=b[0][0];


By the way, is E shd be b2[0][1][0][0] = b[0][0];
 
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Let me explain this to you. Now the answer A is correct. b2 is a 4 dimensional array and b is a 2 dimensional array. There you are assigning a 2d array to b2[0][1]. So the total number of Dimensions are 4. Which makes b2 a 4d array. So that is correct. All you have to remember is that, the number of Dimensions should be the same as to declared once you assign arrays.
Answer B is also correct cause you are just assigning a value (NOT an array) to the 0th location of the 0th row.
Answer C in INCORRECT. The 4th element is an array and NOT a value. But what happens at the Answer C is that it is assigning a value as the 4th Dimension. b2[1][1][0]= b[0][0]; b[0][0] is a value and NOT an array. So now the b2 has only 3 Dimensions, which is WRONG. It should be 4 Dimensions.
Hope you can figure out the rest. Just ask if you need any more clarifications. Cheers.......
 
Kay Liew
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ransika,

Your explanation made better sense. It does reinforce the explaination from the book. Thanks for clearing up my doubts.

k
 
He's dead Jim. Grab his tricorder. I'll get his wallet and this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic