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?