Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes multi dimensional arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "multi dimensional arrays" Watch "multi dimensional arrays" New topic

multi dimensional arrays

tyler jones
Ranch Hand

Joined: Dec 01, 2000
Posts: 101
It's been quite a while since I've done anything with Java and I can't remember if Java has multi dimensional arrays or not? I'm used to using vb, but now that I'm trying to get back into Java, I have a problem to tackle where a two dimensional array would come in handy. If Java does support them, how do I create them? Thanks.
greg philpott
Ranch Hand

Joined: Nov 10, 2000
Posts: 73
yes you can.
like this:
float[][] matrix = new float[4][4];
Jason Moore

Joined: Sep 11, 2001
Posts: 10
Also. . . .
Multidimensional arrays only need specify the first dimension when they are initialized.
int[][][] array = new int[3][][];

for (int i=0; i<array.length; i++)>
array[i] = new int[5][];
for (int j=0; j<array[i].length; j++)>
array[i][j] = new int[1];
Peter Tran

Joined: Jan 02, 2001
Posts: 783
Jason has a good point. Java supports what is called a jagged multi-dimensional arrays. That, the arrays do not have to be in matrix form.
float[][] f = new float[4][3];
float[][] f = new float[4][];
f[0] = new float[4];
f[1] = new float[3];
f[2] = new float[2];
f[3] = new float[1];
This isn't allowed in C/C++ unless you use pointers.
I agree. Here's the link:
subject: multi dimensional arrays
It's not a secret anymore!