GeeCON Prague 2014*
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


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "multi dimensional arrays" Watch "multi dimensional arrays" New topic
Author

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
Greenhorn

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
Bartender

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.
E.g.
float[][] f = new float[4][3];
xxx
xxx
xxx
xxx
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];
xxxx
xxx
xx
x
This isn't allowed in C/C++ unless you use pointers.
-Peter
 
GeeCON Prague 2014
 
subject: multi dimensional arrays