Multi Dimension Arraylist
Hi,
I am trying to come up with a better way to store a score into a table that have X and Y axis.
It's just like 2D array, a score would be stored at the X's row and Y's column and I should be able to get that score using row and column number:
ie.
0 1 2 3 4
1 X X X X
2 X X X X
3 X X X X
4 X X X X
but here is my problem, 2D array work fine when I know the array size, but my input's size
depend on the input file that needs to be loaded into the application and their size are not fixed.
So right now, I end up using multiple ArrayList inside an ArrayList to simulate how the 2D array work, which solve the unfix input's size problem . But it just seem to be quite a messy way to solve this problem for me, so I was just wondering are there any other ways I could go about this?
Cheer!
Re: Multi Dimension Arraylist
In what way do you find it messy? An ArrayList is just a class wrapper around an internal array; it provides auto array-resizing and other facilities to make life simpler. It has to use the method syntax to access the array elements because it's a class.
The outcome of any serious research can only be to make two questions grow where only one grew before...
T. Veblen
Re: Multi Dimension Arraylist
Ah well, may be it's just the way I have to declare and
access the values in the list. I thought it look kinda long
and may be there is a better way to go about this O.o
But never mind, I think I'll just stick to multiple ArrayList.
Re: Multi Dimension Arraylist
Quote:
Ah well, may be it's just the way I have to declare and
access the values in the list.
If it is a real problem for you, you could always convert the ArrayList of ArrayLists to a 2D array once you have read all the values in.
Re: Multi Dimension Arraylist
Quote:
Originally Posted by
holypromise
So right now, I end up using multiple ArrayList inside an ArrayList to simulate how the 2D array work, which solve the unfix input's size problem.
Do you really need a two-dimensional data structure? Can't you work with a one-dimensional data structure and a two-dimensional index function like.
Code:
int index(int row, int col) {
return row*MAX_ROWS + col;
}
You have one ArrayList and use the index function to map row,col pairs on a linear index for access to the ArrayList. This is how accesses to multi-dimensional arrays are handled under the hood in many languages.