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 Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Arrays" Watch "Arrays" New topic
Author

Arrays

Elizabette caldas ferreira
Greenhorn

Joined: Dec 26, 2003
Posts: 7
I am Brazilian, I am studing with java 2 certification sun to programers and developers and I have a doubt, for example, in the chapter 01 test number: 12.
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. }// here!!
8.}
which of the answers could be insert in the line seven and still to permit of the code was compilated?(you have 4 correct answers)
a)b2[0][1] =b;//correct
b)b[0][0] =b3;//correct
c)b2[1][1][0] =b[0][0];//wrong ???
d)b2[1][2][0]=b;//wrong ???
e)b2[0][1][0][0]=b[0][0];//correct
f)b2[0][1]= big;//correct
I didn�t understand why are wrongs ???
Please someone answer to me.....
thank you very much!!

Elizabette Caldas
Bras�lia -DF


;)Liza
K Ville
Ranch Hand

Joined: Aug 16, 2003
Posts: 58

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. }// here!!
8.}
which of the answers could be insert in the line seven and still to permit of the code was compilated?(you have 4 correct answers)
a)b2[0][1] =b;//correct
b)b[0][0] =b3;//correct
c)b2[1][1][0] =b[0][0];//wrong ???
d)b2[1][2][0]=b;//wrong ???
e)b2[0][1][0][0]=b[0][0];//correct
f)b2[0][1]= big;//correct

Let's examine this one by one:
a)b2 is a 4-dimensional array(there are 4 brackets). With option a, it is trying to initialize b2[0][1] with b. In line number 4, you will see that b is a 2-dimensional array(there are 2 brackets). This means that b2[0][1] expects a 2-dimensional array to get completely initialized. If the initialization were changed to b2 = b, the code will not compile because it will state that b2 is a 4-dimensional array and expects a 4-dimensional array size. Hence, if it were changed to b2[0][1][2][0]=b3, then it would be a correcr initialization because all the dimensions were specified, it only needs a byte value to initialize the particular element. Same explanations for all the others.
If it's still a little vague to you, try to count the number of brackets(assuming the elements are not out of bounds). For example, int arr[] = new int[2]; int anotherarr[] = new int[3]; Then it is safe to initialize arr[] with arr = anotherarr regardless of size. Only the dimensions(brackets) are restricted.
I hope you understood my explanation. If you get this, you'll see that it's an easy topic. I got confused the first time I've read about this, too.


Thinking is the talking of the soul with itself...-Plato
sanjana narayanan
Ranch Hand

Joined: Nov 25, 2003
Posts: 142
Hi,

I don't think counting the no of brackets is the correct way because when i tried,
byte [][][]b11 = new byte[3][3][3];
byte b2 [][][][]=new byte[2][3][1][2];
b2[1][1][0] = b11;// #2
i am still getting the error in #2.
Can someone elaborate on the rules.pl..
-Sanjana
Michael Sullivan
Ranch Hand

Joined: Dec 26, 2003
Posts: 235
I believe that your problem is that when you try to assign an array to a pre-declared array, they must be of the same size. So lets have at your code:
byte [][][]b11 = new byte[3][3][3];
// okay, so b11 is a three dimensional array, and each dimension has three elements.
byte b2 [][][][]=new byte[2][3][1][2];
b2[1][1][0] = b11;// #2
here we have b2, a FOUR dimensional array... need we go further? When you assign arrays to each other, they must be of equal size. In this case, b2 is 4D, while b11 is 3d... so compiler error. Does this make sense? It appears that the example is saying that we want to assign the first three array elements in b2 to the array elements in b11, and we should just forget about the fourth element in b2. If the example redeclared b2 as a 3d array, this might work.
[ December 26, 2003: Message edited by: Michael Sullivan ]
sanjana narayanan
Ranch Hand

Joined: Nov 25, 2003
Posts: 142
Hi Michael,
Thanks for replying but u know i am sort of stuck up i this question.Probably i am thinking too much.
a)b2[0][1] =b;//correct
b)b[0][0] =b3;//correct
c)b2[1][1][0] =b[0][0];//wrong ???
d)b2[1][2][0]=b;//wrong ???
e)b2[0][1][0][0]=b[0][0];//correct
f)b2[0][1]= big;//correct
When you say
byte b11[][][] = new byte[2][2][2]
b2[0][1][2] = b11//wrong (because 3D array cannot be assigned to 4D. I accept this)
then how cum the last option f is correct.
It says
b2[0][1] = big. Since b2 is 4D array and big is 2D array, how is this possible.
Please reply.
-Sanjana
Lee Ming
Greenhorn

Joined: Dec 15, 2003
Posts: 16
hi sanjana,
b2 is 4D array, but b2[x][y] is a 2D array!
You may think like this way:
b2 is a 4D array that its elements are byte;
b2 is a 3D array that its elements are 1D array;
b2 is a 2D array that its elements are 2D array;
b2 is a 1D array that its elements are 3D array;
venkatesh rajmendram
Ranch Hand

Joined: Dec 05, 2000
Posts: 130
Hi ,
This might help...
Link
Thanks
Venkatesh


SCJP 1.4, SCWCD<p>Ours is a world where people don't know what they want and are willing to go through hell to get it.<br /> - Don Marquis
venkatesh rajmendram
Ranch Hand

Joined: Dec 05, 2000
Posts: 130
Also try ...Dans Exam
Venkatesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arrays
 
Similar Threads
Can you tell me how to answer such question?
Self Test Question on Arrays by Kathy's Book
Doubt About the output of this Question
Array Reference Assignments
array -please explain the output