Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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.

share|improve this question
    
You should also use UperCamelCase for classnames,is less confusing when you ask a question with sample code as this is the usual style for Java code. (i.e. MainClass, World and Entity) – eckes Mar 29 '16 at 22:34
    
And you most likely want to get rid of Entity() (default constructor) as you will give the entity no name or symbol which is most likely not what you ever want. The other constructor is private, which is also most likely the wrong visibility if you want to use new Entity(0,0,"Entity1", '1') in a different class. – eckes Mar 29 '16 at 22:37
    
Thanks for the tip @eckes! – tfreiner Mar 29 '16 at 23:04
up vote 1 down vote accepted

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

}
share|improve this answer
    
Thanks for your help @starf! – tfreiner Mar 29 '16 at 23:04

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);
share|improve this answer
    
Good call with the inline initialisation. – starf Mar 29 '16 at 22:18

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 }

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.