public class TileGrid implements Iterable<Tile> {
private wheelSize = [a positive integer];
private Tile[][] grid = new Tile[wheelSize * 2 + 1][wheelSize * 2 + 1]
@Override
public Iterator<Tile> iterator() {
return ????????;
}
}
I've made a TileGrid class to keep track of a hex-grid for me. It stores Tile-objects in a two dimensional array called grid. Now I want to make the TileGrid class Iterable so that I can easily loop through all Tile-objects. The problem is that there is some positions in the array that is naturally not used (due to the shape of the hex-grid) and thus contains the value null.
My question is this: How do I create an iterator that iterates through all positions in grid except the ones that is null?
I do not want to use some kind of ArrayList since I'm using the array indexes to mark the position of the Tiles.
TileIterator, that implementsIterator<Tile>and maintains the current position in private fields (e.g.xandy). As you advance the x,y position, you skip empty cells.