Java 相同的数组元素

Java 相同的数组元素,java,arrays,Java,Arrays,我想检查一个数组中的所有元素是否与另一个数组中的所有元素相同,即一对一匹配。(注意:两个数组元素的顺序将不同) 有没有一种快速的方法,或者一个我可以使用的现成的库 目前,我能想到的两种方式并不漂亮: 逐个匹配(编辑3:此示例错误,不适合重复) 排序然后匹配(更优化,尤其是针对大型阵列) 编辑2 “myusuf”指出的一个好问题是数组中的重复项。 这意味着我的“逐个匹配”示例将不起作用。以此为例: public static void main(String[] args) { Stri

我想检查一个数组中的所有元素是否与另一个数组中的所有元素相同,即一对一匹配。(注意:两个数组元素的顺序将不同)

有没有一种快速的方法,或者一个我可以使用的现成的库

目前,我能想到的两种方式并不漂亮:

逐个匹配(编辑3:此示例错误,不适合重复)

排序然后匹配(更优化,尤其是针对大型阵列)


编辑2

“myusuf”指出的一个好问题是数组中的重复项。 这意味着我的“逐个匹配”示例将不起作用。以此为例:

public static void main(String[] args) {
    String[] strList1 = {"ABC", "CDE", "EFG", "ABC"};
    String[] strList2 = {"ABC", "EFG", "CDE", "CDE"};
    System.out.println(arrayEquals(strList1,strList2));
}
private static boolean arrayEquals(String[] array1, String[] array2) {
    List<String> list1 = Arrays.asList(array1);
    List<String> list2 = Arrays.asList(array2);
    return list1.containsAll(list2) && list2.containsAll(list1);
}
我希望有一个方法调用可以完成所有的工作,但似乎已经完成了。 deepEquals()仅在所有元素顺序不同时才适用于排序数组。 使用contains()的方法不起作用,因为存在重复项

Arrays#deepEquals(java.lang.Object[], java.lang.Object[])
它提供了两个数组引用是否完全相等的信息

Arrays.deepEquals(e1,e2);
两个可能为空的元素e1和e2完全相等,如果 以下条件适用:

 - e1 and e2 are both arrays of object reference types, and
   Arrays.deepEquals(e1, e2) would return true
 - e1 and e2 are arrays of the same primitive type, and the appropriate
   overloading of Arrays.equals(e1, e2) would return true.
 - e1 == e2
 - e1.equals(e2) would return true.

Note that this definition permits null elements at any depth.

数组#equals(Object[],Object[])
方法不同,此方法适用于任意深度的嵌套数组

它提供了两个数组引用是否完全相等的信息

Arrays.deepEquals(e1,e2);
两个可能为空的元素e1和e2完全相等,如果 以下条件适用:

 - e1 and e2 are both arrays of object reference types, and
   Arrays.deepEquals(e1, e2) would return true
 - e1 and e2 are arrays of the same primitive type, and the appropriate
   overloading of Arrays.equals(e1, e2) would return true.
 - e1 == e2
 - e1.equals(e2) would return true.

Note that this definition permits null elements at any depth.

数组#equals(Object[],Object[])
方法不同,此方法适用于任意深度的嵌套数组。

使用:

如果指定的两个数组彼此非常相等,则返回true。与@link{#equals{Object[],Object[])方法不同,此方法适合用于任意深度的嵌套数组。 如果两个数组引用都为null,或者如果它们引用的数组包含相同数量的元素,并且两个数组中所有对应的元素对都是深度相等的,则认为这两个数组引用是深度相等的

如果以下任一条件成立,则两个可能为空的元素e1和e2完全相等:

 - e1 and e2 are both arrays of object reference types, and
   Arrays.deepEquals(e1, e2) would return true
 - e1 and e2 are arrays of the same primitive type, and the appropriate
   overloading of Arrays.equals(e1, e2) would return true.
 - e1 == e2
 - e1.equals(e2) would return true.

Note that this definition permits null elements at any depth.
e1和e2都是对象引用类型的数组,并且
数组。deepEquals(e1,e2)
将返回
true

e1和e2是相同基元类型的数组,适当重载
数组。equals(e1,e2)
将返回
true

e1==e2
e1.equals(e2)将返回true

使用:

如果指定的两个数组深度相等,则返回true。与@link{#equals{Object[],Object[])方法不同,此方法适用于任意深度的嵌套数组。 如果两个数组引用都为null,或者如果它们引用的数组包含相同数量的元素,并且两个数组中所有对应的元素对都是深度相等的,则认为这两个数组引用是深度相等的

如果以下任一条件成立,则两个可能为空的元素e1和e2完全相等:

 - e1 and e2 are both arrays of object reference types, and
   Arrays.deepEquals(e1, e2) would return true
 - e1 and e2 are arrays of the same primitive type, and the appropriate
   overloading of Arrays.equals(e1, e2) would return true.
 - e1 == e2
 - e1.equals(e2) would return true.

Note that this definition permits null elements at any depth.
e1和e2都是对象引用类型的数组,并且
数组。deepEquals(e1,e2)
将返回
true

e1和e2是相同基元类型的数组,适当重载
数组。equals(e1,e2)
将返回
true

e1==e2
e1.equals(e2)将返回true

如果两个指定的整数数组彼此相等,则返回true。如果两个数组包含相同数量的元素,则两个数组视为相等,并且两个数组中所有对应的元素对都相等。换句话说,如果两个数组包含相同顺序的相同元素,则两个数组相等。此外,两个数组引用如果两者都为空,则重新视为相等

如果两个指定的整数数组彼此相等,则返回true。如果两个数组包含相同数量的元素,则两个数组视为相等,并且两个数组中所有对应的元素对都相等。换句话说,如果两个数组包含相同顺序的相同元素,则两个数组相等。此外,两个数组引用如果两者都为空,则视为相等。

可以使用列表方法

您可以使用列表方法


您可以使用哈希映射。由于不需要排序和搜索,此方法的时间复杂度较低,但由于使用哈希映射,空间复杂度较高:

        String[] strList1 = {"ABC", "CDE", "EFG"};
        String[] strList2 = {"ABC", "EFG", "CDE"};
        Boolean flag = true;
        Map<String, Integer> map = new HashMap<String, Integer>();

        /*Initial Check*/

        if(strList1.length != strList2.length) {
           System.out.print("Array equals: false");
           return 0;
        }

        /*Add all strings from list1 and their frequency to map*/

        for(int i=0; i<strList1.length;i++) {
            if(map.get(strList1[i]) != null) 
               map.put(strList1[i], map.get(strList1[i]) + 1);
            else
               map.put(strList1[i], 0); 
        }

        /*If string found in map from list2, decrement count in map
          else list2 contains a string not in map, then arrays 
          unequal as list2 contains a string not in list1*/

        for(int i=0; i<strList2.length;i++) {
            if(map.get(strList2[i]) != null)
               map.put(strList2[i], map.get(strList2[i]) - 1);
            else {  
                System.out.print("Array equals: false");
                flag = false;
            }
        }

        /*If map contains the same value for every string i.e. 0, then they occur
          equal no. of times in both arrays. Hence, Set values will contain 
          only one value so (values.size() == 1) will be equal true, else false*/

        if(flag) {
            Set<Integer> values = new HashSet<Integer>(map.values());
            System.out.print("Array equals: " + (values.size() == 1));
        }
        return 0;
String[]strList1={“ABC”、“CDE”、“EFG”};
字符串[]strList2={“ABC”、“EFG”、“CDE”};
布尔标志=真;
Map Map=newhashmap();
/*初步检查*/
if(strList1.length!=strList2.length){
System.out.print(“数组等于:false”);
返回0;
}
/*将列表1中的所有字符串及其频率添加到映射*/

对于(int i=0;i您可以使用哈希映射。此方法的时间复杂度较低,因为不需要排序和搜索,但使用哈希映射的空间复杂度较高:

        String[] strList1 = {"ABC", "CDE", "EFG"};
        String[] strList2 = {"ABC", "EFG", "CDE"};
        Boolean flag = true;
        Map<String, Integer> map = new HashMap<String, Integer>();

        /*Initial Check*/

        if(strList1.length != strList2.length) {
           System.out.print("Array equals: false");
           return 0;
        }

        /*Add all strings from list1 and their frequency to map*/

        for(int i=0; i<strList1.length;i++) {
            if(map.get(strList1[i]) != null) 
               map.put(strList1[i], map.get(strList1[i]) + 1);
            else
               map.put(strList1[i], 0); 
        }

        /*If string found in map from list2, decrement count in map
          else list2 contains a string not in map, then arrays 
          unequal as list2 contains a string not in list1*/

        for(int i=0; i<strList2.length;i++) {
            if(map.get(strList2[i]) != null)
               map.put(strList2[i], map.get(strList2[i]) - 1);
            else {  
                System.out.print("Array equals: false");
                flag = false;
            }
        }

        /*If map contains the same value for every string i.e. 0, then they occur
          equal no. of times in both arrays. Hence, Set values will contain 
          only one value so (values.size() == 1) will be equal true, else false*/

        if(flag) {
            Set<Integer> values = new HashSet<Integer>(map.values());
            System.out.print("Array equals: " + (values.size() == 1));
        }
        return 0;
String[]strList1={“ABC”、“CDE”、“EFG”};
字符串[]strList2={“ABC”、“EFG”、“CDE”};
布尔标志=真;
Map Map=newhashmap();
/*初步检查*/
if(strList1.length!=strList2.length){
System.out.print(“数组等于:false”);
返回0;
}
/*将列表1中的所有字符串及其频率添加到映射*/

对于(int i=0;i这将是您问题的一个简短解决方案。我相信有更多性能更高但更复杂的方法可以实现。这里
arrayquals
将数组转换为
List
并使用
List.containsAll
检查相等性

public class ArrayEquality {
    public static void main(String[] args) {
        String[] array1 = {"test1","test2"};
        String[] array2 = {"test2","test1"};
        String[] array3 = {"test2","test3"};

        String[] strList1 = {"ABC", "CDE", "EFG"};
        String[] strList2 = {"ABC", "EFG", "CDE"};

        System.out.println(arrayEquals(array1,array2));
        System.out.println(arrayEquals(array2,array3));

        System.out.println(arrayEquals(strList1,strList2));
    }

    //ARRAY EQUALITY CHECK
    private static boolean arrayEquals(String[] array1, String[] array2) {
        List<String> list1 = Arrays.asList(array1);
        List<String> list2 = Arrays.asList(array2);

        return list1.containsAll(list2) && list2.containsAll(list1);
    }
}
公共类阵列质量{
公共静态void main(字符串[]args){
字符串[]数组1={“test1”,“test2”};
字符串[]array2={“test2”,“test1”};
字符串[]数组3={“test2”,“test3”};
字符串[]strList1
        String[] strList1 = {"ABC", "CDE", "EFG"};
        String[] strList2 = {"ABC", "EFG", "CDE"};
        Boolean flag = true;
        Map<String, Integer> map = new HashMap<String, Integer>();

        /*Initial Check*/

        if(strList1.length != strList2.length) {
           System.out.print("Array equals: false");
           return 0;
        }

        /*Add all strings from list1 and their frequency to map*/

        for(int i=0; i<strList1.length;i++) {
            if(map.get(strList1[i]) != null) 
               map.put(strList1[i], map.get(strList1[i]) + 1);
            else
               map.put(strList1[i], 0); 
        }

        /*If string found in map from list2, decrement count in map
          else list2 contains a string not in map, then arrays 
          unequal as list2 contains a string not in list1*/

        for(int i=0; i<strList2.length;i++) {
            if(map.get(strList2[i]) != null)
               map.put(strList2[i], map.get(strList2[i]) - 1);
            else {  
                System.out.print("Array equals: false");
                flag = false;
            }
        }

        /*If map contains the same value for every string i.e. 0, then they occur
          equal no. of times in both arrays. Hence, Set values will contain 
          only one value so (values.size() == 1) will be equal true, else false*/

        if(flag) {
            Set<Integer> values = new HashSet<Integer>(map.values());
            System.out.print("Array equals: " + (values.size() == 1));
        }
        return 0;
public class ArrayEquality {
    public static void main(String[] args) {
        String[] array1 = {"test1","test2"};
        String[] array2 = {"test2","test1"};
        String[] array3 = {"test2","test3"};

        String[] strList1 = {"ABC", "CDE", "EFG"};
        String[] strList2 = {"ABC", "EFG", "CDE"};

        System.out.println(arrayEquals(array1,array2));
        System.out.println(arrayEquals(array2,array3));

        System.out.println(arrayEquals(strList1,strList2));
    }

    //ARRAY EQUALITY CHECK
    private static boolean arrayEquals(String[] array1, String[] array2) {
        List<String> list1 = Arrays.asList(array1);
        List<String> list2 = Arrays.asList(array2);

        return list1.containsAll(list2) && list2.containsAll(list1);
    }
}
private static boolean arrayEquals(String[] array1, String[] array2) {
List<String> list1 = Arrays.asList(array1);
List<String> list2 = Arrays.asList(array2);

Collections.sort (list1);
Collections.sort (list2);
return list1.equals(list2);
}
String[] strArray1;
String[] strArray2;
private <T> boolean containsAll(final T[] a1, final T[] a2) {
    return Arrays.asList(a1).containsAll(Arrays.asList(a2));
}
boolean exactMath = containsAll(strArray1, strArray2) 
        && containsAll(strArray2, strArray1);