Java 充当指针的ArrayList内容

Java 充当指针的ArrayList内容,java,arraylist,Java,Arraylist,所以,我有代码为一个对象找到一个最接近0,0的空位。它陷入了一个无限循环,因为ArrayList就像一个指针列表 private static voxel findEmptySpot(){ ArrayList<voxel> open = new ArrayList<voxel>(); ArrayList<voxel> closed = new ArrayList<voxel>(); String isla

所以,我有代码为一个对象找到一个最接近0,0的空位。它陷入了一个无限循环,因为ArrayList就像一个指针列表

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


}
private静态体素findEmptySpot(){
ArrayList open=新建ArrayList();
ArrayList closed=新建ArrayList();
字符串孤岛=null;
添加(新体素(0,0));
做{
体素位置=新体素();
//检查离原点最近的地方
int d=-1;
用于(体素测试:关闭){
SkyCraft.getInstance().getLogger().info(“关闭:“+test.x+”,“+test.y”);
}
用于(体素测试:打开){
int s=abs(试验x)+abs(试验y);
SkyCraft.getInstance().getLogger().info(“位置”+test.x+,“+test.y+“距离”+s+“最佳”+d);
如果(d==-1){
d=s;
位置x=试验x;
位置y=试验y;
}

如果(s我刚刚使用“new”函数创建变量实例,ant然后将这些实例分配给ArrayList。感谢@William Price的帮助。

Java集合都是通过引用创建的。因此如果我使用“new”要在每次向arraylist添加对象时引用新对象,列表中的每个引用都将具有唯一的对象,对吗?是的,每个单独的
new
实例都将是一个单独的、唯一的引用,可以添加到列表中。您的代码还使用了
list.contains()
这不一定与参考质量相同,这取决于被比较对象的类型以及它们的
equals
方法的实现方式。另请参阅