• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Self Test Question on Arrays by Kathy's Book

 
May-Yoong Cheah
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the following,
1. public class Test{
2. public static void main (String[] args){
3. byte [][] big = new byte [7][7];
4. byte [][] b = new byte[2][1];
5. byte b3 = 5;
6. byte b2 [][][][] = new byte [2][3][1][2];
7.
8. }
9. }
which of the following lines of code could be inserted at line 7, and still allow the code to compile? (Choose four that would work.)
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 b2[0][1][0][0] = b[0][0];
F. b2[0][1] = big;
The question is why the answers are A, B, E and F and not C and D? Please explain.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The key here is to understand what datatype you're assigning to what. Java is a strongly typed language. Let's look at a quick example:

You see, the compiler expects that you're going to assign something of type MyObject1 to m1 and m2 (because that's what type of variable they are) but, in the second line, we try to assign something else. At that point, the compiler gets a bit upset and throws an error at you.
So, with that in mind, let's look at your question.
Let's go through each answer and see why it is, or isn't, correct.
A. b2[0][1] = b;
Well, now we have to ask ourselves, what is the data type of b2[0][1]? Well, b2 is a 4-D byte array, so b2[0][1] is a 2-D byte array. Now, what is the type of b? b is also a 2-D byte array, so the assignment works perfectly.
B. b[0][0] = b3;
In this case, b[0][0] is a byte, as is b3, so we're good to go.
C. b2[1][1][0]= b[0][0];
In this case, we have b2[1][1][0], which is an array of bytes (1 dimensional). b[0][0], however, is a byte, not an array of bytes. So, in this instance, you're trying to assign a byte to a reference to an array of bytes. The compiler surely doesn't like that, so this answer can't be correct.
D. b2[1][2][0] = b;
We have a similar problem here. b2[1][2][0] is a 1-D array of bytes, but b is a 2-D array of bytes. Again, you can't assign one to the other, so this answer is incorrect.
I'll let you go through the last couple because there really isn't anything different about them. If you have more questions, let me know.
I hope that helps,
Corey
 
May-Yoong Cheah
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help. But I am still not very clear about the distinction between array of bytes and a byte.
Answer A is ok
But Answer B is not ok to my understanding.
According to you:
B. b[0][0] = b3;
In this case, b[0][0] is a byte, as is b3, so we're good to go.
how come b[0][0] is a byte and not a 2D-array? Obviously, b3 is a byte.
Answer C is not ok so as the rest of the answers.
Why is b2[1][1][0] a 1 dimensional array as I thought it should be a 3D-array?
I think I'm getting more confused. Sorry, hope you do not mind explaining to me like a beginner.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, let's take a quick step back. In my explanation, I'm implying a difference between these two things:

The first line is the declaration of a 4-D byte array. The second line is an index into that array to find a particular value. In this case, we're navigating all the way through that 4-D array to find a single byte.
In order to illustrate, let's start with a simple example (a 1-D array, instead of 4-D). Let's say I have the following code:

Now, let's look at what we've done. We've declared b to be a 1-D array of bytes (5 bytes long). I like to draw pictures, so here's a picture of our array:

But, what datatype do we end up with if you use b[0]? Looking back at our picture, we can see that b[0] is the byte 0. Likewise, b[3] would be the byte 3. b[0] no longer references an array of bytes (b does that), instead, b[0] indexes that array to find something inside it - in this case, a byte.
Now, if we had a 2-D array, like this:

Again, let me draw my picture:

Now, notice that b[0] doesn't represent a byte, it represent another array. In this case, b references a 2-D byte array while b[0] references a 1-D byte array while b[0][0] references a single byte.

Now, knowing that, see if you can make sense of my previous response. I'm sorry if I'm making this unclear, but the best way to explain this is with pictures and pictures aren't so easy to draw on here. :roll: (What I wouldn't give for a whiteboard that you could see...)
I hope that helps,
Corey
[ April 02, 2003: Message edited by: Corey McGlone ]
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May -

According to you:
B. b[0][0] = b3;
In this case, b[0][0] is a byte, as is b3, so we're good to go.
how come b[0][0] is a byte and not a 2D-array? Obviously, b3 is a byte.

May, here is another way to look it at (although I have to admit I draw pictures like Corey - his pictures REALLY ROCK !!)...
If b[0][0] is a 2d array, how would you access one of its elements?
The answer is that 'b' is a 2d array, and b[0][0] is one of its elements, which IS a byte...
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bert Bates:
his pictures REALLY ROCK !!)...

Now, now - let's not get carried away. However, if you spend enough time in this forum, you're bound to see them enough to understand their "inner beauty"... :roll:
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Corey -
A while back I wrote a big, honking long post about G.C. and I 'drew' pictures like yours , it makes me happy to see I'm not the only one who does that, especially happy when a bartender does!
For my money, some of these topics really cry out for pictures!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic