检查2个数组以查看它们是否具有相同的值?

时间:2014-05-07 11:02:32

标签: java arrays

我想比较两个数组,看看它们是否具有相同的值。

如果我有一个名为

的数组
public static float data[][]

保存地形的Y坐标,如何用另一个

检查该数组
public static int coords[][]

没有遍历所有坐标?

两个阵列都有超过1000个值。迭代它们不是一种选择,因为我必须每秒迭代它们四次。

我这样做是为了试图找出两个物体是否发生碰撞。我曾尝试使用库,但我找不到每个坐标的碰撞检测,因为我需要它。

编辑:为什么我无法迭代这个少量的顶点是这样的。 问题是,这是一个MultiPlayer游戏,我必须遍历每个玩家的所有1000个坐标。这意味着在线只有10名玩家是10,000 100在线是10万。你可以看到它会多么容易滞后或者至少占用很大比例的CPU。

将坐标输入到"数据"变量:

try {
        // Load the heightmap-image from its resource file

        BufferedImage heightmapImage = ImageIO.read(new File(
                "res/images/heightmap.bmp"));
        //width = heightmapImage.getWidth();
        //height = heightmapImage.getHeight();
        BufferedImage heightmapColour = ImageIO.read(new File(
                "res/images/colours.bmp"));
        // Initialise the data array, which holds the heights of the
        // heightmap-vertices, with the correct dimensions
        data = new float[heightmapImage.getWidth()][heightmapImage
                .getHeight()];
        // collide = new int[heightmapImage.getWidth()][50][heightmapImage.getHeight()];
        red = new float[heightmapColour.getWidth()][heightmapColour
                .getHeight()];
        blue = new float[heightmapColour.getWidth()][heightmapColour
                .getHeight()];
        green = new float[heightmapColour.getWidth()][heightmapColour
                .getHeight()];
        // Lazily initialise the convenience class for extracting the
        // separate red, green, blue, or alpha channels
        // an int in the default RGB color model and default sRGB
        // colourspace.
        Color colour;
        Color colours;
        // Iterate over the pixels in the image on the x-axis
        for (int z = 0; z < data.length; z++) {
            // Iterate over the pixels in the image on the y-axis
            for (int x = 0; x < data[z].length; x++) {
                colour = new Color(heightmapImage.getRGB(z, x));

                data[z][x] = setHeight;

            }
        }
    }catch (Exception e){
        e.printStackTrace();
        System.exit(1);
    }

如何将坐标放入&#34; coords&#34;变量(哦等等,它被称为&#34; Ship&#34;,而不是coords。我忘记了):

try{
        File f = new File("res/images/coords.txt");
        String coords = readTextFile(f.getAbsolutePath());

        for (int i = 0; i < coords.length();){
            int i1 = i;
            for (; i1 < coords.length(); i1++){
                if (String.valueOf(coords.charAt(i1)).contains(",")){
                    break;
                }
            }
            String x = coords.substring(i, i1).replace(",", "");
            i = i1;
            i1 = i + 1;
            for (; i1 < coords.length(); i1++){
                if (String.valueOf(coords.charAt(i1)).contains(",")){
                    break;
                }
            }
            String y = coords.substring(i, i1).replace(",", "");;
            i = i1;
            i1 = i + 1;
            for (; i1 < coords.length(); i1++){
                if (String.valueOf(coords.charAt(i1)).contains(",")){
                    break;
                }
            }
            String z = coords.substring(i, i1).replace(",", "");;
            i = i1 + 1;
                //buildx.append(String.valueOf(coords.charAt(i)));
                ////System.out.println(x);
                ////System.out.println(y);
                ////System.out.println(z);
                //x = String.valueOf((int)Double.parseDouble(x));
                //y = String.valueOf((int)Double.parseDouble(y));
                //z = String.valueOf((int)Double.parseDouble(z));
            double sx = Double.valueOf(x);
            double sy =  Double.valueOf(y);
            double sz = Double.valueOf(z);
            javax.vecmath.Vector3f cor = new javax.vecmath.Vector3f(Float.parseFloat(x), Float.parseFloat(y), Float.parseFloat(z));
            //if (!arr.contains(cor)){
            if (cor.y > 0)
                arr.add(new javax.vecmath.Vector3f(cor));


            if (!ship.contains(new Vector3f((int) sx, (int) sy, (int) sz)))
                ship.add(new Vector3f((int) sx, (int) sy, (int) sz));

// arr.add(new javax.vecmath.Vector3f(Float.parseFloat(x),Float.parseFloat(y),Float.parseFloat(z)));                 } 谢谢!

3 个答案:

答案 0 :(得分:1)

您可以这样做,但仅适用于相同的数据类型。

 Arrays.deepEquals(data, coords);

对于单维数组,您可以使用此

 Arrays.equals(array1, array1);

答案 1 :(得分:1)

Arrays.deepEquals(a, b);

试试这个,但这只有在元素有序的情况下才有效。

答案 2 :(得分:0)

无所事事,我害怕。根据定义,比较数据集以查看它们是否相同,需要查看所有元素。另外,即使在相对较旧的硬件上,也不会比较1000个值。你每秒可以做几千次。

相关问题