Connecting Tech Pros Worldwide Forums | Help | Site Map

multidimensional array problem

Member
 
Join Date: Oct 2007
Posts: 112
#1: Apr 8 '08
i want to store numbers in a grid. thats not the problem...i need to be able to store a variable amount of numbers at each location...originally i set up an int[][][] but the inflexibility of the array made some problems for me...what i need is something like this: int[][]() the ()being an arraylist or something of that nature. Something about this confuses me...is there a way to do that?

thanks

BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#2: Apr 8 '08

re: multidimensional array problem


Quote:

Originally Posted by drsmooth

i want to store numbers in a grid. thats not the problem...i need to be able to store a variable amount of numbers at each location...originally i set up an int[][][] but the inflexibility of the array made some problems for me...what i need is something like this: int[][]() the ()being an arraylist or something of that nature. Something about this confuses me...is there a way to do that?

thanks

You can have an array of any type, including an array of lists. You can also define arrays piecemeal, like this:

Expand|Select|Wrap|Line Numbers
  1. int size = 5;
  2. int [][] triangle = new int[size][];
  3. for(int i=0; i<size; ++i) {
  4.     triangle[i] = new int[i+1];
  5. }
It's not clear from your post what your real problem is. If it's that you want an array to grow or shrink, that's usually a sign that you should go with a collection like a list instead.
Member
 
Join Date: Oct 2007
Posts: 112
#3: Apr 8 '08

re: multidimensional array problem


yea ill try the arraylist way...thats wat i thought to do i just wasnt sure how it would work...wud i use grid[x][y].get(z)? i guess that makes sense

thanks alot
BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#4: Apr 8 '08

re: multidimensional array problem


Quote:

Originally Posted by drsmooth

yea ill try the arraylist way...thats wat i thought to do i just wasnt sure how it would work...wud i use grid[x][y].get(z)? i guess that makes sense

thanks alot

I'm not a big fan of mixing arrays and collections. Actually, I think if you analyze your problem further you'll want to start defining some classes. I've never had to nest arrays three deep: [][][]. Unless this is some specific math problem, I suppose, but I can only guess your domain.
Reply