我有两个可能有重复的数组.我需要将它们作为集合进行比较.
例如{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++;} …Run Code Online (Sandbox Code Playgroud)