• 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

multi-dimensional array help

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to get a grasp on multi-dimensional arrays and am having a bit of trouble. Here is a question from the book that I would like some help understanding. The correct answer is a,b,e,f but I am not really getting why. Can anyone help me out?
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 4 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;
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jarlax King:
I am trying to get a grasp on multi-dimensional arrays and am having a bit of trouble. Here is a question from the book that I would like some help understanding. The correct answer is a,b,e,f but I am not really getting why. Can anyone help me out?
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 4 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;


You can look at multidimensional arrays as being an array of arrays. The left and right side of the assignment operator should be compatible.
Considering the answer choices,
-b2 is a four dimensional array. It needs 4 indices to effectively point to an element of type byte. Giving only 2 indices, points to a 2 Dimensional sub-array. b2[0][1] refers to a 2D array (same as b which is also a 2D array), which is why answer A is right.
-b[0][0] refers to a byte element, which is choice B
-b2[1][1][0] refers to a 1D array but b[0][0] on the RHS is a single byte element.This raises a compiler error.
- b2[1][2][0] again refers to a 1D array but b (on the RHS) is a 2D array. Causes a compiler error.
b2[0][1][0][0] points to a single byte element and is compatible with the RHS b[0][0], which also refers to a single byte element.
-b2[0][1] refers to a 2D array and is compatible with big, also a 2D array.
[ May 12, 2004: Message edited by: V Bose ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me try to help you out here. It's essential to determine what data type each variable refers to. Let's start with what's declared.
big is of type byte[][]
b is of type byte[][]
b3 is of type byte
b2 is of type byte[][][][]
Now, let's look at each of the possible answers. We'll start with A:
A. b2[0][1] = b;
The applicable question here is what is the type of b2[0][1]? Recall that b2 is a 4-dimensional array of bytes. Therefore, b[0] is a 3-dimensional array of bytes and b2[0][1] is a 2-dimensional array of bytes. The variable b is also of type byte[][] (a 2-dimensional array of bytes), so this assignment is valid. Choice "A" is correct.
B. b[0][0] = b3;
The variable b refers to a 2-dimensional array. Therefore, b[0] refers to a one-dimensional array and b[0][0] refers to a single byte. The variable b3 is also a single byte so this assignment is valid. Choice "B" is correct.
C. b2[1][1][0] = b[0][0];
The variable b2 refers to a 4-dimensional byte array. Therefore b[1] is a 3-dimensional array, b[1][1] is a 2-dimensional array, and b[1][1][0] is a 1-dimensional array. The variable b refers to a 2-dimensional array so b[0] is a 1-dimensional array and b[0][0] is just a byte. You can't assign a byte to an array of bytes so this line is invalid. Option "C" is not correct.
Hopefully, that'll help you along the way. I'll leave the last few for you to do. If you're still confused, let me know.
Corey
 
Jarlax King
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys, I think I get it now! Below is my hard to understand attempt at visually explaining the logic.
What is being declared:
-----------------------
big is of type byte[][]
b is of type byte[][]
b3 is of type byte
b2 is of type byte[][][][]
The questions and answers:
--------------------------
A. b2[0][1] = b;
In this case, the result after the assignment would look something like this:
b2 [][reference var b][array b[] inserted] [byte value in b[][]inserted] - OK
B. b[0][0] = b3;
Here the result would be:
b[][byte value of b3] - OK
C. b2[1][1][0] = b[0][0];
Here:
attempt to assign byte value where byte array is expected, exception thrown
D. b2[1][2][0] = b;
Here:
b2[][][reference var b] [array b[] inserted] attempt to insert an array value where a byte is expected, exception thrown.
E. b2[0][1][0][0]=b[0][0];
Here:
b2[][][][byte value in b[][] inserted] - OK
F. b2[0][1]=big;
b2[][reference var big inserted][big[] inserted][big[][] inserted] - OK
 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic