简体   繁体   English

Java对象数组初始化

[英]Java Object Array Initialization

I am working on a java project which contains 3 classes and an object array in one of the classes. 我正在一个Java项目中,该项目包含3个类,其中一个类包含一个对象数组。 This project is ultimately supposed to move 4 entity objects around on a board by using the coordinates of the entity objects. 最终,该项目应该通过使用实体对象的坐标在板上移动4个实体对象。 These entity objects are stored in an array in the world class. 这些实体对象存储在world类的数组中。 My problem is with the array initialization in the world class. 我的问题是世界类中的数组初始化。 I am not sure how to set each element of the array equal to an object from the entity class and then access that object's coordinates to move it around on the board. 我不确定如何将数组的每个元素设置为与实体类中的对象相等,然后访问该对象的坐标以在板上移动它。 The coordinates for the entity objects are initially set at 20x30 in a default constructor. 实体对象的坐标最初在默认构造函数中设置为20x30。 Here is my code: 这是我的代码:

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();
    }

}

My draw function and main function are not finished. 我的绘画功能和主功能尚未完成。 After the array is initialized I will be able to finish the draw method using the entity get functions. 数组初始化之后,我将能够使用实体get函数完成draw方法。 Thanks for your help. 谢谢你的帮助。

You simply need to initialise the array. 您只需要初始化数组即可。 This can be done in the world constructor. 这可以在world构造函数中完成。

public world()
{

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

}

Then you can access the objects in your draw method, as you've shown: 然后,您可以在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);
    }
}

A more complete example, with the move functions added, and the class names capitalised: 一个更完整的示例,其中添加了move函数,并且类名大写:

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);
        }
    }

}

That is one way of doing it. 那是做到这一点的一种方式。 You can also define all of your entities inline like this: 您还可以像这样内联定义所有实体:

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')
};

A better way may be to do an ArrayList instead of an array: 更好的方法可能是执行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');

To access each element you just need to get the element from the array and either get or set the properties you need: 要访问每个元素,您只需要从数组中获取元素并获取或设置所需的属性即可:

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

Your problem is only creating new object of world inside world's constructor which throws stack overflow error, otherwise it is fine: 您的问题是仅在world的构造函数中创建world的新对象,这会引发堆栈溢出错误,否则就可以了:

public world(){ world test = new world(); 公共世界(){世界测试=新世界(); //REMOVE THIS LINE } //删除此行}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM