我有两个数组,可能会有重复的元素。我需要将它们作为集合进行比较。
例如,
例如,
{1, 4, 9, 16, 9, 7, 4, 9, 11}
等同于 {11, 11, 7, 9, 16, 4, 1}
。我已经尝试了很多方法,但是一直得到错误或错误的答案。以下是我现在的代码:import java.util.Scanner;
public class sameElement{
public static void main(String[] args){
int[] value1 = {11, 7, 9, 16, 4, 1};
int[] value2 = {11, 11, 7, 9, 16, 4, 1};
sort(value1);
sort(value2);
System.out.println(sameSet(value1, value2));
}
public static boolean sameSet(int[] a, int[] b){
int j = 0;
int counter2 = 0;
for(int i = 0; i < b.length; i++){
if(a[j] == b[i]){j++;}
else{counter2++;};}
}
public static int[] sort (int[] a){
for (int i = 0; i < a.length; i++) {
for (int i2 = i + 1; i2 < a.length; i2++){
if (a[i] > a[i2]){
int temp = a[i2];
a[i2] = a[i];
a[i] = temp;}
}
}
return a;
}
}