首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ArrayList removeAll()不移除

ArrayList removeAll()不移除
EN

Stack Overflow用户
提问于 2014-08-06 00:50:16
回答 1查看 2.8K关注 0票数 0

我有一个小问题,我的数组列表中的元素没有删除。这是一个ArrayList。这是我的密码:

代码语言:javascript
复制
package net.lucrecious.armorconstruct.helpers;

import java.util.ArrayList;

import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;

public class WorldHelper {

    public static void replace_coord_with_block(World world, ArrayList<int[]> coords, Block result_block){

        for (int[] c : coords){
            world.setBlock(c[0], c[1], c[2], result_block);
        }

    }

    public static ArrayList<int[]> get_blocks_in_sphere(EntityPlayer player, World world, int radius, Class<?>...block_types){

        ArrayList<int[]> coord_list = new ArrayList<int[]>();

        int r = radius;

        int i = MathHelper.floor_double(player.posX);
        int j = MathHelper.floor_double(player.posY); 
        int k = MathHelper.floor_double(player.posZ);
        for(int x = -r; x < r; x++){
            for(int y = -r; y < r; y++){ 
                for(int z = -r; z < r; z++){                    
                    double dist = MathHelper.sqrt_double((x*x + y*y + z*z)); //Calculates the distance

                    if(dist > r)
                        continue;

                    Block block = world.getBlock(i+x, j+y, k+z);

                    for (Class<?> cls : block_types){
                        if (cls.isInstance(block)){
                            coord_list.add(new int[]{i+x, j+y, k+z});
                        }
                    }
                }
            }
        }

        return coord_list;
    }


    public static ArrayList<int[]> get_blocks_in_sphere_holo(EntityPlayer player, World world, int radius, Class<?>...block_types){

        ArrayList<int[]> sphere = get_blocks_in_sphere(player, world, radius, block_types);

        ArrayList<int[]> inner_sphere = get_blocks_in_sphere(player, world, radius-1, block_types);

        sphere.removeAll(inner_sphere);

        return sphere;
    }

    public static ArrayList<int[]> get_blocks_in_sphere_filled(EntityPlayer player, World world, int radius, Class<?>...block_types){
        return get_blocks_in_sphere(player, world, radius, block_types);
    }


}

其思想是,得到一个填充球的坐标,然后抓取一个稍小的填充球的坐标,并移除类似的坐标,从而有效地形成一个未填充的球体。

我知道里面有一些“我的世界”的代码--但它还是可以理解的。我也肯定会有相似的坐标。我甚至在半径没有减少的情况下尝试了这个,但是这个仍然不起作用。所以即使所有的坐标都一样,这也是行不通的。

对于是什么原因造成的,或者如何让它发挥作用,有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-06 00:56:07

调用removeAll()时,java将从列表中删除与作为参数传递的列表中的任何条目相等的条目。问题是,两个数组永远不会相等,除非它们是完全相同的数组实例,而在您的代码中它们不是。

您可以使用一个包装类来包含您的int数组,它将重写equals()方法,并使用Arrays.equals(array1, array2)来比较两个数组是否相等。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25150648

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档