如何从 Java 中的常规数组创建迭代器?

How do I create an iterator from a regular array in Java?

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 class 来为我跟踪六角网格。它将 Tile 个对象存储在一个名为 grid 的二维数组中。现在我想制作 TileGrid class Iterable 以便我可以轻松地遍历所有 Tile 对象。问题是数组中有一些位置自然不会被使用(由于十六进制网格的形状),因此包含值 null

我的问题是:如何创建迭代器来遍历 grid 中除 null 之外的所有位置?

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

您必须 return 一个迭代器实现的实例 class。您 return 的迭代器应该能够访问您的数组,这样代码才有意义。(http://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html)

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

这意味着您需要编写一个 class 来实现迭代器接口并实现该接口的 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:感谢您的提问,这对我来说是一项有趣的任务。

@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);
    }
}

再次感谢您的回答,因为它帮助我完成了这个。