-1

So, I have code to find an empty spot closest to 0,0 for an object. It is falling into an infinite loop because the ArrayList is acting like a list of pointers.

        private static voxel findEmptySpot(){

    ArrayList<voxel> open = new ArrayList<voxel>();
    ArrayList<voxel> closed = new ArrayList<voxel>();

    String island = null;

    open.add(new voxel(0,0));

    do{

        voxel pos = new voxel();

        //Check for place closest to origin
        int d = -1;

        for(voxel test: closed){
            SkyCraft.getInstance().getLogger().info("Closed: "+test.x+","+test.y);
        }

        for(voxel test: open){
            int s = abs(test.x)+abs(test.y);
            SkyCraft.getInstance().getLogger().info("Position "+test.x+", "+test.y+" distance "+s+" best "+d);
            if(d==-1){
                d = s;
                pos.x = test.x;
                pos.y = test.y;
            }
            if(s<d){
                d = s;
                pos.x = test.x;
                pos.y = test.y;
            }
        }

        SkyCraft.getInstance().getLogger().info("Closest found; testing.");

        Island i = SkyCraft.db().getIsland(pos.x+";"+pos.y);

        if(i.owner!=null){
            if(i.owner.equals("")){
                return pos;
            }else{
                voxel x1 = new voxel(pos.x+1, pos.y);
                voxel x2 = new voxel(pos.x-1, pos.y);
                voxel y1 = new voxel(pos.x, pos.y+1);
                voxel y2 = new voxel(pos.x, pos.y-1);
                if(!closed.contains(new voxel(pos.x+1, pos.y))){
                    if(!open.contains(new voxel(pos.x+1, pos.y))){
                        open.add(new voxel(pos.x+1, pos.y));
                    }
                }
                if(!closed.contains(new voxel(pos.x-1, pos.y))){
                    if(!open.contains(new voxel(pos.x-1, pos.y))){
                        open.add(new voxel(pos.x-1, pos.y));
                    }
                }
                if(!closed.contains(new voxel(pos.x, pos.y+1))){
                    if(!open.contains(new voxel(pos.x, pos.y+1))){
                        open.add(new voxel(pos.x, pos.y+1));
                    }
                }
                if(!closed.contains(new voxel(pos.x, pos.y-1))){
                    if(!open.contains(new voxel(pos.x, pos.y-1))){
                        open.add(new voxel(pos.x, pos.y-1));
                    }
                }
                open.remove(pos);
                closed.add(pos);

            }
        }else{
            return pos;
        }

    }while(true);


}

When the values of the list closed print, the values are all equal to whatever the variable pos is at that time. It is like a list of pointers from C++. How can I get the ArrayList to actually contain the objects themselves, not the references to the objects?

Mad3ngineer
  • 113
  • 2
  • 11
  • Java collections are all by-reference. – William Price Apr 26 '14 at 20:20
  • So if I use "new" to reference a new object every time I add an object to the arraylist, it will have unique objects for each reference in the list, correct? – Mad3ngineer Apr 26 '14 at 20:26
  • 1
    Yes, every separate `new` instance will be a separate, unique reference that can be added to the list. Your code is also using `List.contains()` which is [defined in terms of object equality](http://docs.oracle.com/javase/7/docs/api/java/util/List.html#contains%28java.lang.Object%29) which is not necessarily the same as reference quality, depending on the type of objects being compared and how their `equals` method is implemented. Also look at http://stackoverflow.com/questions/2642589/how-does-a-arraylists-contains-method-evaluate-objects – William Price Apr 26 '14 at 20:31

1 Answers1

-1

I just used the "new" function to create instances of variables, ant then assigned those to the ArrayList. Thanks @William Price for the help.

Mad3ngineer
  • 113
  • 2
  • 11