![](/img/trans.png)
[英]This program checks whether two arrays have the same elements in some order
[英]Check whether two arrays have same elements in some order
用户需要输入两个数组(数组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)
。
就是说“包含”方法不存在。 您的代码中没有“包含方法”。
包含??? 按照我的说法,您要么需要声明此方法,要么需要使用Sets(其中将“ contains”作为有效操作提供)
更好的是使用集来满足您的需求
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.