1
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 ????????;
    }
}

我已经开设了一TileGrid门课程来为我跟踪六角网格。它将Tile-objects 存储在一个名为 的二维数组中grid。现在我想创建这个TileGridIterable,以便我可以轻松地遍历所有Tile对象。问题是数组中有一些位置自然不使用(由于六角网格的形状),因此包含值null

我的问题是:我如何创建一个迭代器,它遍历grid除那些之外的所有位置null

我不想使用某种 ArrayList,因为我使用数组索引来标记 Tiles 的位置。

4

2 回答 2

2

您必须返回 Iterator 类的实现的实例。您返回的迭代器应该能够访问您的数组,以便代码有意义。(http://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html

public Iterator<Tile> iterator() {
   return new TileGridIterator(grid);
}

这意味着您需要编写一个实现 Iterator-Interface 并实现该接口的 API 中指定的所有方法的类。

这方面的一个例子可能如下所示:

import java.util.Iterator;
import java.util.NoSuchElementException;

public class TileGridIterator implements Iterator<Tile> {
    int x = 0;
    int y = 0;
    int nextX = 0;
    int nextY = -1;
    Tile[][] grid;

    public TileGridIterator(Tile[][] grid) {
        this.grid = grid;
    }

    public boolean hasNext() {
        while(nextX <= x && nextY < y) {
            nextY++;
            if(nextY == grid[nextX].length) {
               nextY = 0;
               nextX++;
            }
            if(nextX >= grid.length) {
                return false;
            }
            if(grid[nextX][nextY] != null) {
                return true;
            }
        }
        if(nextX < grid.length && nextY < grid[nextX].length && grid[nextX][nextY] != null) {
            return true;
        }
        else {
            return false;
        }
    }

    public Tile next() {
        if(hasNext()) {
            x = nextX;
            y = nextY;
            return grid[x][y];
        }else {
            throw new NoSuchElementException("no more elements left");
        }
    }
}

ps:谢谢你的问题,这对我来说是一个有趣的任务。

于 2015-08-17T17:25:12.603 回答
1

@HopefullyHelpful

我的版本:

public Iterator<Tile> iterator() {
    return new TileIterator(grid);
}

.

class TileIterator implements Iterator<Tile> {

    int x = 0, y = -1;
    int newX, newY;
    Tile[][] grid;

    TileIterator(Tile[][] grid) {
        this.grid = grid;
        updateNewIndex();
    }

    public boolean hasNext() {
        if (newX == -1) {
            return false;
        }
        return true;
    }

    public Tile next() {
        x = newX;
        y = newY;
        updateNewIndex();
        if (x == -1) {
            throw new NoSuchElementException("no more elements left");
        }
        return grid[x][y];
    }

    private void updateNewIndex() {
        newX = x;
        newY = y;
        do {
            newY++;
            if (newY == grid[newX].length) {
                newY = 0;
                newX = newX + 1;
                if (newX == grid.length) {
                    newX = newY = -1;
                }
            }
        } while (newX != -1 && grid[newX][newY] == null);
    }
}

再次感谢您的回答,因为它帮助我做到了这一点。

于 2015-08-18T16:25:43.463 回答