java - 检查两个数组是否按某种顺序具有相同的元素

标签 java arrays

用户需要输入两个数组(数组 a 和数组 b),然后这两个数组需要运行一个方法来查看它们是否按某种顺序包含相同的元素,忽略重复项。

不过我遇到了一些麻烦,我在 sameSet() 方法中不断收到“找不到符号 - 方法包含 (int[], int)”错误。

有人可以告诉我为什么会发生这种情况并指出我正确的方向吗?

 /**
 * check whether two arrays have the same elements in some order, ignoring
 * duplicates. For example, the two arrays 1 4 9 16 9 7 4 9 11 and 11 11 7 9
 * 16 4 1 would be considered identical.
 */

 public static boolean sameSet(int[] a, int sizeA, int[] b, int sizeB) {
    boolean sameSets = true; 

    for (int i = 0; i < a.length; i++) {
        if (!contains(b, a[i])) { // This line right here is where I'm having trouble. I get "Cannot symbol - method contains (int[], int)" 
            sameSets = false;
        }
    }
    for (int i = 0; i < b.length; i++) {
        if (!contains(a, b[i])) { // This line also has the same problem as the above.
            sameSets = false;
        }
    }
    return sameSets;
}


// main method used to collect user input, then pass input to sameSet method

    public static void main() { 

    final int LENGTH = 10;
    int sizeA = 0;
    int sizeB = 0;
    int a[] = new int[LENGTH];
    int b[] = new int[LENGTH];

    Scanner input = new Scanner(System.in);
    Scanner in = new Scanner(System.in);

    System.out.println("Please fill up values for array a, Q to quit: ");
    while (input.hasNextInt() && sizeA < a.length) {
        a[sizeA] = input.nextInt();
        sizeA++;

    }

    System.out.println("Please fill up values for array b, type in Q to quit: ");
    while (in.hasNextInt() && sizeB < b.length) {
        b[sizeB] = in.nextInt();
        sizeB++;
    }
    for (int i : b) {
        System.out.print(i + " | "); // For testing

    }

   sameSet(a, sizeA, b, sizeB);

}

最佳答案

您可以创建 HashSet来自每个数组,然后使用 set1.equals(set2)

关于java - 检查两个数组是否按某种顺序具有相同的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28551138/

相关文章:

Java:Group By 然后 Map

java - 将 Cucumber 集成到测试框架中?

java - JSOUP + 多部分/表单数据响应

PHP/MYSQL 到 SESSION - 做什么

arrays - Swift - 对数组的子集进行排序

java - Tomcat 8 和 Websocket

java - Java TicTacToe 游戏中的行和列搜索

javascript - 使用 node-soap 传递整数数组

arrays - Solidity 中字符串到数组的转换

java - 如何从已弃用的 Date 类型替换 getDate()?