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

TileGrid16 進グリッドを追跡するためのクラスを作成しました。Tileオブジェクトを という 2 次元配列に格納しgridます。ここで、すべてのオブジェクトを簡単にループできるようにTileGridクラスを作成したいと思います。問題は、(16 進グリッドの形状のために) 当然使用されない配列内のいくつかの位置があり、値が含まれていることです。IterableTilenull

grid私の質問は次のとおりです: の位置を除くすべての位置を反復処理するイテレータを作成するにはどうすればよいnullですか?

タイルの位置をマークするために配列インデックスを使用しているため、ある種の ArrayList を使用したくありません。

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

@希望に満ちたヘルプフル

私のバージョン:

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 に答える