简体   繁体   中英

Java Object Array Initialization

I am working on a java project which contains 3 classes and an object array in one of the classes. This project is ultimately supposed to move 4 entity objects around on a board by using the coordinates of the entity objects. These entity objects are stored in an array in the world class. 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. 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. Thanks for your help.

You simply need to initialise the array. This can be done in the world constructor.

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:

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:

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:

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:

public world(){ world test = new world(); //REMOVE THIS LINE }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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