当前位置: 首页 > 知识库问答 >
问题:

检查2个数组是否有相同的值?

谢胤
2023-03-14

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

如果我有一个数组,名为

public static float data[][]
public static int coords[][]
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);
    }

以及如何将坐标放入“coords”变量(哦,等等,它被称为“ship”,而不是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));}谢谢!

共有1个答案

郎弘业
2023-03-14
Arrays.deepEquals(a, b);

尝试这样做,但这将工作,只有当元素是有序的。

 类似资料:
  • 如何检查两个数组(循环)是否具有相同顺序的相同元素。例如,让我们以数组[1,2,3,4]为例。 对于[2,3,4,1]、[3,4,1,2]、[4,1,2,3],测试应返回true,但对于[1,3,2,4]、[1,4,2,3]或[1,2,3,5]则不返回true。 我最初的方法是找到第一个匹配项——每个数组中一个相等的元素——并将这两个元素视为各自数组的初始元素,我逐个比较了数组的其余元素。 有没有

  • 我有一个String类型的数组列表和一个Person类型的数组列表。其中,Person是一个仅包含包含名称的字符串的对象。 假设我这样做, 假设创建一个新的Person对象会将名称设置为“Josh”,并假设Person类具有该名称的get方法。 有没有办法检查名称数组列表中是否包含名为Josh的人。 我唯一能想到的就是这个, 现在,如果Person数组列表和names数组列表包含多个元素,如何检查

  • 问题内容: 我想检查两个数组是否相等。我的意思是:相同的大小,相同的索引,相同的值。我怎样才能做到这一点? 根据用户的建议,如果数组中的至少一个元素不同,我希望以下内容可以打印 enter ,但实际上没有。 问题答案: $arraysAreEqual = ($a == $b); // TRUE if $a and $b have the same key/value pairs. $arraysA

  • 问题内容: 我知道我可以这样做: 然后只需编写语句中所需的代码。 还有其他方法可以检查它们是否相等? 问题答案: 怎么了 if(!Arrays.equals(array1,array2)) 与相同,即是同一数组。这不是大多数人期望的。 比较数组的内容。

  • 嗨,我正在尝试解决Udemy练习:编写一个名为hasSharedDigit的方法,其中包含int类型的两个参数。 每个数字应在10(含)-99(含)之间。如果其中一个数字不在范围内,则该方法应返回false。 如果两个数字中都有数字,例如12和23中的2,则该方法应返回true;否则,该方法应返回false。 我一直在得到真实,而有共享数字(9,99)我无法发现为什么.. }