0

次のコードが与えられます:

public class Game {



    private Map<String,Coordinate> m_myHashMap;
... // more code 
}

public Game(Display display,int level) 
{
        this.m_myHashMap = new HashMap<String,Coordinate>();
... // more code 

}

クラス座標

パッケージモデル;

public class Coordinate {

    private int xCoordinate;
    private int yCoordinate;
    private int sign;

    public Coordinate(int x,int y)
    {
        this.xCoordinate = x;
        this.yCoordinate = y;
        this.sign = 0;
    }

    public int getXCoordinate()
    {
        return this.xCoordinate;
    }

    public int getYCoordinate()
    {
        return this.yCoordinate;
    }

    public void setSign(int number)
    {
        this.sign = number;
    }

    public int getSign()
    {
        return this.sign;
    }

    public void setXcoordinate(int newX)
    {
        this.xCoordinate = newX;

    }

    public void setYcoordinate(int newY)
    {
        this.yCoordinate = newY;

    }

}

そしてゲームクラスのメソッド:

private void placeTreasuresInMaze(PaintEvent e)
{
    // e.gc.drawPolygon(new int[] { 25+x,5+y,45+x,45+y,5+x,45+y });
    int numberOfTreasures = this.m_numberOfPlayers * 3;  // calculate number of treasures 
    Set set = this.m_myHashMap.entrySet();
    Iterator iterator = set.iterator();     

    while (numberOfTreasures > 0 && iterator.hasNext())
    {
        numberOfTreasures--;
        // need to add more code here 

    }
}

HashMapイテレータがないので、ハッシュマップの要素を取得するためにSetを使用しました。私の問題は、の値を繰り返したいときに始まりましたHashMapが、それが不可能なため、で試しましSetたが、オブジェクト自体ではなく、をSet返します。それを取得する方法はありますか?ObjectCoordinate

よろしく、ロン

4

1 に答える 1

4

問題は、セットとイテレータにraw型を使用していることです。ジェネリック型を使用する必要があります。

Iterator<Map.Entry<String, Coordinate>> iterator = m_myHashMap.entrySet()
                                                              .iterator();

または:

Set<Map.Entry<String, Coordinate>> set = this.m_myHashMap.entrySet();
Iterator<Map.Entry<String, Coordinate>> iterator = set.iterator();     

一方、本当にマップの値を繰り返し処理したい場合は、次ようにします。

Iterator<Coordinate> valueIterator = this.m_myHashMap.values().iterator();
于 2012-01-28T13:49:07.733 に答える