gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:12:18 24 4
gpt4 key购买 nike

用户需要输入两个数组(数组 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com