Arrays deepToString()
i got this question from one of the mock tests. And says only 2 & 3 will compile.
import java.util.*;
class DumpArray{
publicstaticvoid main(String [] args){
int [] a ={7,9,8};
int [][] aa ={{1,2,3},{6,5,4}};
int [][][] aaa ={{{1,2},{3,4}},{{5,6},{7,8}}};
System.out.println(Arrays.deepToString(a));// 1
System.out.println(Arrays.deepToString(aa));// 2
System.out.println(Arrays.deepToString(aaa));//3
}
}
But, according to me nothing should compile. Because, deepToString method takes Object[] as parameter. As arrays of primitive types are not sub types of Object[] but Object.
Even if a, aa,aaa are arrays of Integer class, 1 and 2 should compile. as the 3 dimension array is not properly initialized.
Please suggest.
Thanks
[1875 byte] By [
ramkria] at [2007-11-27 9:37:50]

a is an instance of java.lang.Object.
So let me see if I get this right.
It's obvious that int[] is an array of primitives, not an array of objects, therefore not an Object[].
The tricky point is that int[][] is an array of int[], and since int[] is an object (subtype of Object), int[][] can be regarded as an array of Objects. Therefore 2 compiles.
A similar argument goes for 3. int[][][] is an array of int[][] and so forth.
It sounds right to myself :-)
OleVVa at 2007-7-12 23:09:57 >

> the 3 dimension array is not properly initialized.Yes it is. It's just that the array has one element in it. Like int[] eg = {42};
int [][][] aaa = {{{1,2}, {3,4}}, {{5,6},{7,8}}};
The way I read this line, I see no sub-array with only one element in it. The array is 2 by 2 by 2, isn't it?
> Please suggest.
Did you try running the sample program through your compiler?
Message was edited by:
OleVV
OleVVa at 2007-7-12 23:09:57 >

> The way I read this line, I see no sub-array with only one element in it. The array is> 2 by 2 by 2, isn't it?Yes. My previous post was nonsense. Time to go home...
Hi All
my argument here is that
deepToString(Object[] o) takes array of type Object as parameter. Since arrays of int (primitive) are not sub-types to Object[] but to Object itself, all the 3 lines shouldn't compile.
I've also noticed that there are only 2 elements in [][][] dimension array. This also should cause compile error. Please tryu and compile this on your system and provide correct answer.
Thanks
> I've also noticed that there are only 2 elements in
> [][][] dimension array. This also should cause
> compile error. Please tryu and compile this on your
> system and provide correct answer.
I didn't get the last part. You're too lazy to compile the program yourself and therefore ask the rest of us to do it for you?
OleVVa at 2007-7-12 23:09:57 >

> Hi All
>
> my argument here is that
> deepToString(Object[] o) takes array of type
> Object as parameter. Since arrays of int (primitive)
> are not sub-types to Object[] but to Object itself,
> all the 3 lines shouldn't compile.
Wrong.
So let me see if I get this right.
It's obvious that int[] is an array of primitives, not an array of objects, therefore not an Object[].
The tricky point is that int[][] is an array of int[], and since int[] is an object (subtype of Object), int[][] can be regarded as an array of Objects. Therefore 2 compiles.
A similar argument goes for 3. int[][][] is an array of int[][] and so forth.
It sounds right to myself :-)
This one is brilliant
Message was edited by:
mohammedsajid
I didn't get the last part. You're too lazy to compile the program yourself and therefore ask the rest of us to do it for you?
Stop this ****. I did compile on my system and i'm getting compiler error on the 3 dimentional array. But the answer to that question says it doesn't compile the line marked as 1. I'm just looking for the reasoning.
As per my observation, i should give compiler error on 4 lines.
int[][][] aaa
//1
//2
//3
i've specified why is that in my earlier posting. plz clarify whether its correct or not.
Thanks
> > I didn't get the last part. You're too lazy to
> > compile the program yourself and therefore ask the
> > rest of us to do it for you?
>
> Stop this ****.
Excuse me?
> I did compile on my system and i'm
> getting compiler error on the 3 dimentional array.
> But the answer to that question says it doesn't
> compile the line marked as 1. I'm just looking for
> the reasoning.
>
> As per my observation, i should give compiler error
> on 4 lines.
>
> int[][][] aaa
>
> //1
> //2
> //3
>
> i've specified why is that in my earlier posting. plz
> clarify whether its correct or not.
>
> Thanks
The following does compile. Period.
import java.util.*;
class DumpArray {
public static void main(String [] args) {
int [] a = {7,9,8};
int [][] aa = {{1,2,3}, {6,5,4}};
int [][][] aaa = {{{1,2}, {3,4}}, {{5,6},{7,8}}};
//System.out.println(Arrays.deepToString(a));// 1
System.out.println(Arrays.deepToString(aa));// 2
System.out.println(Arrays.deepToString(aaa)); //3
}
}
> Stop this ****. I did compile on my system and i'm
I'm confused now. I don't think you're talking to me in any nice way (though the forum is kind enough to substitute your four letter word with four asterisks). And I think you're contradicting yourself: Would you want me to stop posting to this thread or would you want me to take part in the attempt to clarify what is going on? I'm asking you, but I give no guarantee that I will go by what you want either way. I do guarantee that if I do take part in the game, I will do it my way.
Thank you for answering my question. There was no way I could have known the answer before, so I still think I did the right thing by asking it. Your answer did contribute to the clarification.
OleVVa at 2007-7-12 23:09:58 >

> my argument here is that
> deepToString(Object[] o) takes array of type Object as
> parameter. Since arrays of int (primitive) are not sub-types
> to Object[] but to Object itself, all the 3 lines shouldn't
> compile.
From the API doc:
This method is designed for converting multidimensional arrays to strings.
and:
If an element e is an array of a primitive type, it is converted to a string as by invoking the appropriate overloading of Arrays.toString(e). If an element e is an array of a reference type, it is converted to a string as by invoking this method recursively.