File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reg :- Java Multi Dimension Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Reg :- Java Multi Dimension Array" Watch "Reg :- Java Multi Dimension Array" New topic
Author

Reg :- Java Multi Dimension Array

RAGU KANNAN
Ranch Hand

Joined: Dec 16, 2005
Posts: 103
I couldn't undersand the answer for the following questions.
Why the anser "C & D " are wrong?


Question :-
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;

Answer:-
12. A, B, E, and F. This question covers the issue of, �What can I assign to an array reference
variable?� The key is to get the dimensions right. For example, if an array is declared as a
two-dimensional array, you can�t assign a one-dimensional array to a one-dimensional array
reference.
C is wrong because it tries to assign a primitive byte where a byte array (one dimension) is
expected. D is wrong because it tries to assign a two-dimensional array where a one-dimensional
array is expected.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
It tells you why c and d are wrong at the bottom of the answer:

C is wrong because it tries to assign a primitive byte where a byte array (one dimension) is expected. D is wrong because it tries to assign a two-dimensional array where a one-dimensional array is expected.


So what's not clear?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
RAGU KANNAN
Ranch Hand

Joined: Dec 16, 2005
Posts: 103
Thanks for the reply Mr.Barry.
If b[0][0] it is two dimentision array like that b2[1][1][0] is three dimentision array. but answer says one dimention array, how it's one dimentision array?

C. b2[1][1][0] = b[0][0];
D. b2[1][2][0] = b;

Thanks, Raghu.K
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

b2 is a "4-dimensional" byte array, instantiated with the dimensions [2][3][1][2]. In Java, multi-dimensional arrays are really just arrays of arrays.

In particular, for int values w, x, y, and z within range, b2[w] represents a 3-d array, b2[w][x] represents a 2-d array, b2[w][x][y] represents a 1-d array, and b2[w][x][y][z] represents a byte.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reg :- Java Multi Dimension Array
 
Similar Threads
Arrays
Sierra and Bates Master Exam Question Question
Array Reference Assignments
scjp1.4 qtn on Mult-dimensional array
Regarding assiging a reference to array object