0

我已经声明了一个 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);
        }
    }       

此时如何拉回 Location 的第一个元素?我想要那个特定位置在 x y 处的数值。

干杯!

4

1 回答 1

2

该代码locations[x][y]为您提供了一个Location,因此只需在此处调用正确的方法:

locationID = locations[x][y].locationId();
于 2013-05-15T22:09:07.537 回答