Re: Multidimensional arrays and arrays of arrays
On Jan 16, 3:16 am, Arne Vajh=F8j <a...@vajhoej.dk> wrote:
Daniel Pitts wrote:
How can I distinguish if an array is multidimensional of if it just
contains an array? Or put it another way how can I tell at runtime
whether a/b in the example below is an array containing an array, or
if it is a multidimensional array.
There is no difference between a multidimensional array, and an array o=
f
arrays. They are the same thing. Java provides a little syntactic sugar
for allocation a uniform array of arrays (of arrays etc...), but there
is no other difference, and no way to tell, other than testing each
element of every array of arrays.
Or we can say that Java does not have multidimensional arrays.
Depending on ones definition of multidimensional array.
That's what I also thought at first.
But my first example precisely showing that there _is_ a difference
between an multidimensional array and an array of arrays, they are not
of the same class type. The same code again, ready to compile and
which emphasizes that classes of the two objects a and b anre not the
same.
public class ATest {
public static void main(String[] args) {
Object[] a = new Object[1];
a[0] = new float[12];
Object b = new float[1][12];
System.out.println(a.getClass()); // prints "class
[Ljava.lang.Object;"
System.out.println(b.getClass()); // prints "class [[F"
if(a instanceof float[][]){
System.out.println("a is float[][]"); // does not print anything
}
if(b instanceof float[][]){
System.out.println("b is float[][]"); // prints "b is float[][]"
}
}
}