Multidimensional arrays and arrays of arrays

P

Philipp

public class TwoDim {
     public static void main(String[] args) {
         float[][] a = new float[3][3];
         float[][] b = new float[3][];
         b[0] = new float[2];
         b[1] = new float[3];
         b[2] = new float[4];
         System.out.println(a.getClass().getName());
         System.out.println(b.getClass().getName());
         System.out.println(a instanceof float[][]);
         System.out.println(b instanceof float[][]);
     }

}

outputs:

[[F
[[F
true
true

float[][] a = new float[3][3];

is not a true two dimensional array - it is a quick way
to create an array of arrays when all the second level
arrays have the same length.

Thank you for that clarification. I didn't know that you could create
2D arrays where different elements in the second dimension have
different sizes.
So I agree that there is no real multidimensional array in Java - you
always have to deal with each element separately, testing it for null
etc.

Thanks to all for taking the time to clarify all this stuff about
arrays.
Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top