比较java中的两个整数数组

时间:2021-02-05 12:18:39

I am trying to write code to compare two arrays. In the first array I have put my own digits, but the second the array takes numbers from the input file. The size of this array is determined by the first number in the file while the first array is always of size 10. The length must be the same for both arrays as well as the numbers. My code is below:

我正在编写代码来比较两个数组。在第一个数组中,我放置了自己的数字,但是第二个数组从输入文件中获取数字。这个数组的大小由文件中的第一个数字决定,而第一个数组的大小总是为10。对于数组和数字,长度必须相同。我的代码如下:

public static void compareArrays(int[] array1, int[] array2) {
    boolean b = false;
    for (int i = 0; i < array2.length; i++) {

        for (int a = 0; a < array1.length; a++) {

            if (array2[i] == array1[a]) {
                b = true;
                System.out.println("true");
            } else {
                b = false;
                System.out.println("False");
                break;
            }
        }
    }       
}

10 个解决方案

#1


19  

public static void compareArrays(int[] array1, int[] array2) {
        boolean b = true;
        if (array1 != null && array2 != null){
          if (array1.length != array2.length)
              b = false;
          else
              for (int i = 0; i < array2.length; i++) {
                  if (array2[i] != array1[i]) {
                      b = false;    
                  }                 
            }
        }else{
          b = false;
        }
        System.out.println(b);
    }

#2


57  

From what I see you just try to see if they are equal, if this is true, just go with something like this:

从我所看到的你们试着看看它们是否相等,如果这是真的,就像这样:

boolean areEqual = Arrays.equals(arr1, arr2);

This is the standard way of doing it.

这是标准的做法。

Ops, it seams that the arrays must be also sorted to be considered equal, from the java doc:

根据java doc,数组也必须进行排序才能被认为是相等的:

"Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. In other words, two arrays are equal if they contain the same elements in the same order"

如果两个数组包含相同数量的元素,并且两个数组中所有相应的元素对都是相等的,那么两个数组就被认为是相等的。换句话说,如果两个数组以相同的顺序包含相同的元素,那么两个数组就是相等的"

Sorry for missing that.

抱歉失踪。

#3


31  

use
Arrays.equals(ary1,ary2); // returns boolean value

使用Arrays.equals(ary1 ary2);/ /返回布尔值

EDIT
you can use Arrays.deepEquals(ary1,ary2) to compare 2D arrays as well

编辑您也可以使用arrays . deepequals (ary1,ary2)来比较2D数组

also check this link for comparision comparision between Arrays.equls(ar1,ar2) and Arrays.deepEquals(ar1,ar2)

还要检查这个链接,查看Arrays.equls(ar1,ar2)和Arrays.deepEquals(ar1,ar2)之间的比较。

Java Arrays.equals() returns false for two dimensional arrays

为二维数组返回false

EDIT 2
if you dont want to use these library methods then you can easily implement your method like this:

编辑2如果你不想使用这些库方法,那么你可以很容易地实现你的方法如下:

public static boolean ArrayCompare(int[] a, int[] a2) {
    if (a==a2)   // checks for same array reference
        return true;
    if (a==null || a2==null)  // checks for null arrays
        return false;

    int length = a.length;
    if (a2.length != length)  // arrays should be of equal length
        return false;

    for (int i=0; i<length; i++)  // compare array values
        if (a[i] != a2[i])
            return false;

    return true;
}

#4


6  

If you know the arrays are of the same size it is provably faster to sort then compare

如果您知道数组的大小相同,那么排序和比较的速度显然更快

Arrays.sort(array1)
Arrays.sort(array2)
return Arrays.equals(array1, array2)

If you do not want to change the order of the data in the arrays then do a System.arraycopy first.

如果您不想改变数组中的数据的顺序,那么请执行一个系统。arraycopy第一。

#5


2  

None of the existing answers involve using a comparator, and therefore cannot be used in binary trees or for sorting. So I'm just gonna leave this here:

现有的答案中没有一个涉及使用比较器,因此不能用于二叉树或排序。所以我把它放在这里

public static int compareIntArrays(int[] a, int[] b) {
    if (a == null) {
        return b == null ? 0 : -1;
    }
    if (b == null) {
        return 1;
    }
    int cmp = a.length - b.length;
    if (cmp != 0) {
        return cmp;
    }
    for (int i = 0; i < a.length; i++) {
        cmp = Integer.compare(a[i], b[i]);
        if (cmp != 0) {
            return cmp;
        }
    }
    return 0;
}

#6


1  

You can check array equality with the Apache Commons ArrayUtils#isEquals() method.

您可以使用Apache Commons ArrayUtils#isEquals()方法检查数组是否相等。

#7


1  

Even though there is something easy like .equals, I'd like to point out TWO mistakes you made in your code. The first: when you go through the arrays, you say b is true or false. Then you start again to check, because of the for-loop. But each time you are giving b a value. So, no matter what happens, the value b gets set to is always the value of the LAST for-loop. Next time, set boolean b = true, if equal = true, do nothing, if equal = false, b=false.

尽管有一些简单的东西,我想指出你在代码中犯的两个错误。第一个:当你遍历数组时,你说b是真还是假。然后再开始检查,因为有for循环。但是每次你给b一个值。无论发生什么,b的值总是最后一个for循环的值。下次,设置布尔b= true,如果= true,什么都不做,如果= =false, b=false。

Secondly, you are now checking each value in array1 with each value in array2. If I understand correctly, you only need to check the values at the same location in the array, meaning you should have deleted the second for-loop and check like this: if (array2[i] == array1[i]). Then your code should function as well.

其次,您现在正在用array2中的每个值检查array1中的每个值。如果我理解正确,您只需检查数组中相同位置的值,这意味着您应该删除第二个for循环,并像这样检查:If (array2[I] = array1[I])。那么您的代码也应该发挥作用。

Your code would work like this:

你的代码可以这样工作:

public static void compareArrays(int[] array1, int[] array2) {
    boolean b = true;
    for (int i = 0; i < array2.length; i++) {
        if (array2[i] == array1[i]) {
            System.out.println("true");
        } else {
            b = false;
            System.out.println("False");
        }
    } 
    return b;

}

}

But as said by other, easier would be: Arrays.equals(ary1,ary2);

但正如其他人所说,更简单的是:Arrays.equals(ary1,ary2);

#8


1  

The length of the arrays must be the same and the numbers just be the same throughout(1st number in arrays must be the sasme and so on)

数组的长度必须是相同的,并且数字是相同的(数组中的第一个数字必须是sasme等)

Based on this comment, then you already have your algorithm:

基于这个评论,你已经有了你的算法:

  1. Check if both arrays have the same length:

    检查两个数组的长度是否相同:

    array1.length == array2.length

    array1。长度= = array2.length

  2. The numbers must be the same in the same position:

    数字必须在相同的位置相同:

    array1[x] == array2[x]

    array1[x]= = array2[x]

Knowing this, you can create your code like this (this is not Java code, it's an algorithm):

了解了这一点,您可以创建这样的代码(这不是Java代码,而是算法):

function compareArrays(int[] array1, int[] array2) {

    if (array1 == null) return false
    if (array2 == null) return false

    if array1.length != array2.length then return false

    for i <- 0 to array1.length - 1
        if array1[i] != array2[i] return false

    return true
}

Note: your function should return a boolean, not being a void, then recover the return value in another variable and use it to print the message "true" or "false":

注意:您的函数应该返回一个布尔值,而不是空值,然后在另一个变量中恢复返回值,并使用它打印消息“true”或“false”:

public static void main(String[] args) {
    int[] array1;
    int[] array2;
    //initialize the arrays...
    //fill the arrays with items...
    //call the compare function
    boolean arrayEquality = compareArrays(array1, array2);
    if (arrayEquality) {
        System.out.println("arrays are equals");
    } else {
        System.out.println("arrays are not equals");
    }
}

#9


0  

Here my approach,it may be useful to others.

在这里,我的方法可能对其他人有用。

public static void compareArrays(int[] array1, int[] array2) {
    if (array1.length != array2.length)
    {
           System.out.println("Not Equal");
    }
    else
    {
        int temp = 0;
        for (int i = 0; i < array2.length; i++) {  //Take any one of the array size
            temp^ = array1[i] ^ array2[i];   //with help of xor operator to find two array are equal or not                 
        }
        if( temp == 0 )
        {
             System.out.println("Equal");
        }
        else{
             System.out.println("Not Equal");
        }
    }
}

#10


-2  

For the sake of completeness, you should have a method which can check all arrays:

为了完整起见,您应该有一个可以检查所有数组的方法:

    public static <E> boolean compareArrays(E[] array1, E[] array2) {
      boolean b = true;
      for (int i = 0; i < array2.length; i++) {
        if (array2[i].equals(array1[i]) ) {// For String Compare
           System.out.println("true");
        } else {
           b = false;
           System.out.println("False");
        }
      } 
      return b;
    }

#1


19  

public static void compareArrays(int[] array1, int[] array2) {
        boolean b = true;
        if (array1 != null && array2 != null){
          if (array1.length != array2.length)
              b = false;
          else
              for (int i = 0; i < array2.length; i++) {
                  if (array2[i] != array1[i]) {
                      b = false;    
                  }                 
            }
        }else{
          b = false;
        }
        System.out.println(b);
    }

#2


57  

From what I see you just try to see if they are equal, if this is true, just go with something like this:

从我所看到的你们试着看看它们是否相等,如果这是真的,就像这样:

boolean areEqual = Arrays.equals(arr1, arr2);

This is the standard way of doing it.

这是标准的做法。

Ops, it seams that the arrays must be also sorted to be considered equal, from the java doc:

根据java doc,数组也必须进行排序才能被认为是相等的:

"Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. In other words, two arrays are equal if they contain the same elements in the same order"

如果两个数组包含相同数量的元素,并且两个数组中所有相应的元素对都是相等的,那么两个数组就被认为是相等的。换句话说,如果两个数组以相同的顺序包含相同的元素,那么两个数组就是相等的"

Sorry for missing that.

抱歉失踪。

#3


31  

use
Arrays.equals(ary1,ary2); // returns boolean value

使用Arrays.equals(ary1 ary2);/ /返回布尔值

EDIT
you can use Arrays.deepEquals(ary1,ary2) to compare 2D arrays as well

编辑您也可以使用arrays . deepequals (ary1,ary2)来比较2D数组

also check this link for comparision comparision between Arrays.equls(ar1,ar2) and Arrays.deepEquals(ar1,ar2)

还要检查这个链接,查看Arrays.equls(ar1,ar2)和Arrays.deepEquals(ar1,ar2)之间的比较。

Java Arrays.equals() returns false for two dimensional arrays

为二维数组返回false

EDIT 2
if you dont want to use these library methods then you can easily implement your method like this:

编辑2如果你不想使用这些库方法,那么你可以很容易地实现你的方法如下:

public static boolean ArrayCompare(int[] a, int[] a2) {
    if (a==a2)   // checks for same array reference
        return true;
    if (a==null || a2==null)  // checks for null arrays
        return false;

    int length = a.length;
    if (a2.length != length)  // arrays should be of equal length
        return false;

    for (int i=0; i<length; i++)  // compare array values
        if (a[i] != a2[i])
            return false;

    return true;
}

#4


6  

If you know the arrays are of the same size it is provably faster to sort then compare

如果您知道数组的大小相同,那么排序和比较的速度显然更快

Arrays.sort(array1)
Arrays.sort(array2)
return Arrays.equals(array1, array2)

If you do not want to change the order of the data in the arrays then do a System.arraycopy first.

如果您不想改变数组中的数据的顺序,那么请执行一个系统。arraycopy第一。

#5


2  

None of the existing answers involve using a comparator, and therefore cannot be used in binary trees or for sorting. So I'm just gonna leave this here:

现有的答案中没有一个涉及使用比较器,因此不能用于二叉树或排序。所以我把它放在这里

public static int compareIntArrays(int[] a, int[] b) {
    if (a == null) {
        return b == null ? 0 : -1;
    }
    if (b == null) {
        return 1;
    }
    int cmp = a.length - b.length;
    if (cmp != 0) {
        return cmp;
    }
    for (int i = 0; i < a.length; i++) {
        cmp = Integer.compare(a[i], b[i]);
        if (cmp != 0) {
            return cmp;
        }
    }
    return 0;
}

#6


1  

You can check array equality with the Apache Commons ArrayUtils#isEquals() method.

您可以使用Apache Commons ArrayUtils#isEquals()方法检查数组是否相等。

#7


1  

Even though there is something easy like .equals, I'd like to point out TWO mistakes you made in your code. The first: when you go through the arrays, you say b is true or false. Then you start again to check, because of the for-loop. But each time you are giving b a value. So, no matter what happens, the value b gets set to is always the value of the LAST for-loop. Next time, set boolean b = true, if equal = true, do nothing, if equal = false, b=false.

尽管有一些简单的东西,我想指出你在代码中犯的两个错误。第一个:当你遍历数组时,你说b是真还是假。然后再开始检查,因为有for循环。但是每次你给b一个值。无论发生什么,b的值总是最后一个for循环的值。下次,设置布尔b= true,如果= true,什么都不做,如果= =false, b=false。

Secondly, you are now checking each value in array1 with each value in array2. If I understand correctly, you only need to check the values at the same location in the array, meaning you should have deleted the second for-loop and check like this: if (array2[i] == array1[i]). Then your code should function as well.

其次,您现在正在用array2中的每个值检查array1中的每个值。如果我理解正确,您只需检查数组中相同位置的值,这意味着您应该删除第二个for循环,并像这样检查:If (array2[I] = array1[I])。那么您的代码也应该发挥作用。

Your code would work like this:

你的代码可以这样工作:

public static void compareArrays(int[] array1, int[] array2) {
    boolean b = true;
    for (int i = 0; i < array2.length; i++) {
        if (array2[i] == array1[i]) {
            System.out.println("true");
        } else {
            b = false;
            System.out.println("False");
        }
    } 
    return b;

}

}

But as said by other, easier would be: Arrays.equals(ary1,ary2);

但正如其他人所说,更简单的是:Arrays.equals(ary1,ary2);

#8


1  

The length of the arrays must be the same and the numbers just be the same throughout(1st number in arrays must be the sasme and so on)

数组的长度必须是相同的,并且数字是相同的(数组中的第一个数字必须是sasme等)

Based on this comment, then you already have your algorithm:

基于这个评论,你已经有了你的算法:

  1. Check if both arrays have the same length:

    检查两个数组的长度是否相同:

    array1.length == array2.length

    array1。长度= = array2.length

  2. The numbers must be the same in the same position:

    数字必须在相同的位置相同:

    array1[x] == array2[x]

    array1[x]= = array2[x]

Knowing this, you can create your code like this (this is not Java code, it's an algorithm):

了解了这一点,您可以创建这样的代码(这不是Java代码,而是算法):

function compareArrays(int[] array1, int[] array2) {

    if (array1 == null) return false
    if (array2 == null) return false

    if array1.length != array2.length then return false

    for i <- 0 to array1.length - 1
        if array1[i] != array2[i] return false

    return true
}

Note: your function should return a boolean, not being a void, then recover the return value in another variable and use it to print the message "true" or "false":

注意:您的函数应该返回一个布尔值,而不是空值,然后在另一个变量中恢复返回值,并使用它打印消息“true”或“false”:

public static void main(String[] args) {
    int[] array1;
    int[] array2;
    //initialize the arrays...
    //fill the arrays with items...
    //call the compare function
    boolean arrayEquality = compareArrays(array1, array2);
    if (arrayEquality) {
        System.out.println("arrays are equals");
    } else {
        System.out.println("arrays are not equals");
    }
}

#9


0  

Here my approach,it may be useful to others.

在这里,我的方法可能对其他人有用。

public static void compareArrays(int[] array1, int[] array2) {
    if (array1.length != array2.length)
    {
           System.out.println("Not Equal");
    }
    else
    {
        int temp = 0;
        for (int i = 0; i < array2.length; i++) {  //Take any one of the array size
            temp^ = array1[i] ^ array2[i];   //with help of xor operator to find two array are equal or not                 
        }
        if( temp == 0 )
        {
             System.out.println("Equal");
        }
        else{
             System.out.println("Not Equal");
        }
    }
}

#10


-2  

For the sake of completeness, you should have a method which can check all arrays:

为了完整起见,您应该有一个可以检查所有数组的方法:

    public static <E> boolean compareArrays(E[] array1, E[] array2) {
      boolean b = true;
      for (int i = 0; i < array2.length; i++) {
        if (array2[i].equals(array1[i]) ) {// For String Compare
           System.out.println("true");
        } else {
           b = false;
           System.out.println("False");
        }
      } 
      return b;
    }