Java对象数组初始化

时间:2016-03-29 22:00:10

标签: java arrays class object

我正在研究一个java项目,其中包含3个类和一个类中的对象数组。该项目最终应该通过使用实体对象的坐标在板上移动4个实体对象。这些实体对象存储在世界类的数组中。我的问题是世界级的数组初始化。我不确定如何将数组的每个元素设置为等于实体类中的对象,然后访问该对象的坐标以在板上移动它。实体对象的坐标最初在默认构造函数中设置为20x30。这是我的代码:

public class entity {

    private int xcoordinate;
    private int ycoordinate;
    private String name;
    private char symbol;

    public entity(){
        xcoordinate = 20;
        ycoordinate = 30;
    }

    private entity(int newxcoor, int newycoor, String newname, char newsymbol){
        xcoordinate = newxcoor;
        ycoordinate = newycoor;
        name = newname;
        symbol = newsymbol;
    }

    public int getXCoor(){
        return xcoordinate;
    }

    public int getYCoor(){
        return ycoordinate;
    }

}

public class world {

    private entity[] ObArray = new entity[4];

    public world(){
        world test = new world();
    }

    public void draw(){
        for (int i = 0; i < 4; i++)
        {
            //int x = ObArray[i].getXLoc();
            //int y = ObArray[i].getYLoc();
        }
    }

}

public class mainclass {

    public static void main(String[] args){
        world worldob = new world();
        //entity a = new entity();
        //entity b = new entity();
        //entity c = new entity();
        //entity d = new entity();
        worldob.draw();
    }

}

我的绘图功能和主要功能尚未完成。在初始化数组之后,我将能够使用实体获取函数完成绘制方法。 谢谢你的帮助。

3 个答案:

答案 0 :(得分:1)

您只需要初始化数组。这可以在world构造函数中完成。

public world()
{

    for (int i = 0; i < 4; i++)
    {
        ObArray[i] = new entity();
    }

}

然后您可以访问draw方法中的对象,如您所示:

public void draw()
{
    for (int i = 0; i < 4; i++)
    {
        int x = ObArray[i].getXCoor();
        int y = ObArray[i].getYCoor();

        System.out.println("x" + x);
        System.out.println("y" + y);

        // Manipulate items in the array
        // ObArray[i].setXCoor(10);
    }
}

一个更完整的例子,添加了移动函数,并且类名大写:

public class Entity
{

    private int xcoordinate;
    private int ycoordinate;
    private String name;
    private char symbol;

    public Entity()
    {
        xcoordinate = 20;
        ycoordinate = 30;
    }

    private Entity(int newxcoor, int newycoor, String newname, char newsymbol)
    {
        xcoordinate = newxcoor;
        ycoordinate = newycoor;
        name = newname;
        symbol = newsymbol;
    }

    public int getXCoor()
    {
        return xcoordinate;
    }

    public void setXCoor(int xcoordinate)
    {
        this.xcoordinate = xcoordinate;
    }

    public int getYCoor()
    {
        return ycoordinate;
    }

    public void setYcoor(int ycoordinate)
    {
        this.ycoordinate = ycoordinate;
    }

    public static void main(String[] args)
    {
        World worldob = new World();

        worldob.draw();

        worldob.move(0, 15, 30);
        worldob.move(1, 45, 0);
        worldob.move(2, 23, 27);
        worldob.move(3, 72, 80);

        worldob.draw();
    }

}

class World
{

    private final Entity[] ObArray;

    public World()
    {
        this.ObArray = new Entity[4];

        for (int i = 0; i < ObArray.length; i++)
        {
            ObArray[i] = new Entity();
        }

    }

    public void move(int index, int xCoor, int yCoor)
    {
        if (index >= 0 && index < ObArray.length)
        {
            Entity e = ObArray[index];
            e.setXCoor(xCoor);
            e.setYcoor(yCoor);
        }
    }

    public void draw()
    {
        for (Entity e : ObArray)
        {
            int x = e.getXCoor();
            int y = e.getYCoor();
            System.out.println("x" + x);
            System.out.println("y" + y);
        }
    }

}

答案 1 :(得分:1)

这是一种做法。您还可以像这样内联定义所有实体:

private entity[] ObArray = {
    new entity(0,0,"Entity1",'a'),
    new entity(10,10,"Entity2",'b'),
    new entity(20,20,"Entity3",'c'),
    new entity(30,30,"Entity4",'d')
};

更好的方法是使用ArrayList而不是数组:

private List<entity> ObArray = new ArrayList<>();

ObArray.add(new entity(0,0,"Entity1",'a');
ObArray.add(new entity(10,10,"Entity2",'b');
ObArray.add(new entity(20,20,"Entity3",'c');
ObArray.add(new entity(30,30,"Entity4",'d');

要访问每个元素,只需从数组中获取元素,然后获取或设置所需的属性:

ObArray[0].getXCoor();
ObArray[0].setXCoor(5);

答案 2 :(得分:1)

你的问题只是在世界的构造函数中创建了一个新的对象,它会抛出堆栈溢出错误,否则就可以了:

public world(){world test = new world(); //删除此行     }