This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

multi dimensional arrays

 
tyler jones
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes you can.
like this:
float[][] matrix = new float[4][4];
 
Jason Moore
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic