如何进行双数组枚举查找

时间:2013-05-15 22:05:57

标签: java arrays enums coordinates lookup

我已经声明了一个id和描述的枚举以及坐标的双数组。

public enum Location {
    GRASS(0, "Green Grass"),
    CHURCH(1, "A church");

    private int locationId;
    public int locationId() { return locationId; }

    private String description;
    public String details(){ return description; }

    Location(int locationId, String description){
        this.locationId = locationId;
        this.description = description;
    }

然后我宣布了我的位置数组......

private Location[][] locations;

然后我为地图的宽度和高度设置了大量随机位置...

    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            locations[x][y] = Math.random() < 0.5 ? Location.GRASS: Location.CHURCH;
        }
    }

现在我想循环遍历这些位置并从tiles [x] [y]中拉出locationId

    int locationID;
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            locationID = locations[x][y].WHATGOESHERE?
            Debug.e("Current location: " + locationID);
        }
    }       

此时如何撤回位置的第一个元素?我希望该特定位置的数值为x y。

干杯!

1 个答案:

答案 0 :(得分:2)

代码locations[x][y]为您提供Location,因此请在此处调用正确的方法:

locationID = locations[x][y].locationId();