开始使用枚举时出现越界异常

Out of bounds exception when started using enums

我正在尝试制作一个程序来绘制 java 中的地形图。刚才它在主程序中标记错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Map.run(Map.java:35) at Terrain.run(Terrain.java:31) at Terrain.main(Terrain.java:10)

我刚开始使用枚举,当我在我的代码中实现一些时它才开始标记。这是主要的 class:

import java.util.Scanner;

public class Terrain {

    public static void main(String[] args) {

        Terrain t = new Terrain();

        t.init();
        t.run();//Flags here
    }
    private Map map;

    public void init()
    {
        map = new Map();
        Scanner in = new Scanner(System.in);
        System.out.println("Please enter a height");
        map.setHeight(in.nextInt());
        System.out.println("Please enter a width");
        map.setWidth(in.nextInt());
        System.out.println("Please enter a hedge size");
        map.setHedgeSize(in.nextInt());
        System.out.println("Please enter an entrance size");
        map.setEntranceSize((in.nextInt()/2));
        in.close();
    }

    public void run()
    {
        map.run();//And flags here
    }
}

这是地图 class:

public class Map {

    private int height;
    private int width;
    private int area;
    private int passCount;
    private int hedgeSize;
    private int entranceSize;
    private int percentageOfPassable;
    TerrainType [][] terrain;
    TerrainType tile;

    public Map()
    {
    height=0;
    width=0;
    area=0;
    passCount=0;
    hedgeSize=0;
    entranceSize=0;
    percentageOfPassable=0;
    terrain = new TerrainType[width][height];

    }
    public void run()
    {
        for (int y=0; y<height; y++)
        {
            String mapDisplay = new String();
            for (int x=0; x<width; x++)
            {
                area++; 
                terrain[x][y]=TerrainType.GRASS;
                if (x<hedgeSize||x>((width-1)-hedgeSize))
                {
                    terrain[x][y]=TerrainType.HEDGE;
                }
                if (y<hedgeSize||y>((height-1)-hedgeSize))
                {
                    terrain[x][y]=TerrainType.HEDGE;
                }//Sets border with regard to hedge size
                if (y>((height-1)-hedgeSize))
                {
                    if (x>=((width/2)-entranceSize)&&x<((width/2)+entranceSize))
                    {
                        terrain[x][y]=TerrainType.GRASS;
                    }
                }//Entrance size+position with regard to hedge size

                if(terrain[x][y].getTerrainPassable())
                {
                    passCount++;
                    terrain[x][y]=TerrainType.GRASS;
                }
                mapDisplay+=terrain[x][y].getChar();
            }
            System.out.println(mapDisplay);
        }//End nested for loop to set initial values
        percentageOfPassable=passCount*100/area;
        System.out.println(percentageOfPassable+"%");
    }

    public void setHeight(int height)
    {
        this.height=height;
    }
    public void setWidth(int width)
    {
        this.width=width;
    }
    public void setEntranceSize(int entranceSize)
    {
        this.entranceSize=entranceSize;
    }
    public void setHedgeSize(int hedgeSize)
    {
        this.hedgeSize=hedgeSize;
    }
}

最后是我的枚举:

import java.awt.Color;

public enum TerrainType
{
    GRASS(',', true, Color.GREEN), ROCK('#', false, Color.GRAY),
    HEDGE('H', false, Color.YELLOW), WATER('W', false, Color.BLUE);
     private char terrainChar;
     private boolean terrainPassable;
     private Color colour;

     private TerrainType(char terrainChar, boolean terrainPassable, Color colour) {
            this.terrainChar = terrainChar;
            this.terrainPassable = terrainPassable;
            this.colour = colour;
        }

        public char getChar() {
            return terrainChar;
        }
        public boolean getTerrainPassable() {
            return terrainPassable;
        }
        public Color getColour() {
            return colour;
        }
}

这是因为您的地形在构造函数中被初始化为 TerrainType[0][0],这导致数组为空,并且在用户输入所需的 运行() 期间没有重新初始化值因此它保持为空数组。

height = 0;
width = 0;
terrain = new TerrainType[width][height];

您可以将其移至 Map.java 上的 运行() 方法,这样我就可以生成一个大小由用户指定的数组。

public void run() {
    terrain = new TerrainType[width][height];
    ...
}