There is a list of objects I need to add to a grid without receiving an IndexOutOfBoundsException. Each object has two numbers associated with it which corresponds to it’s index and column position in a grid. There can be 3 columns and unlimited rows. I need to call add() method for this grid but only in the correct order, so :
(0,0),(0,1),(0,2),(1,0)…
The grid would therefore look like this:
0 1 2
0 x x x
1 x x x
2 x x x
3 x x x
I must also take into account the chance that no object exists for a certain position. For example:
A) x x x B) x x C) x x x
x x x x x x x
x x x x
x x x x
x x
Can this be done? I’m not sure where to start.
Maybe you should think about another datastructure that stores objects by (row,column). It’s interface would look like
And than you can use a list of lists to store the data.
List<List<Object>>or, as Mark Peters suggests, a sparse matrixIf working with the cell values is important, add a cell iterator method. A simple implementation would look like: