【问题标题】:Find same multiplicities in two arrays在两个数组中查找相同的重数
【发布时间】:2015-02-17 03:47:45
【问题描述】:

我需要有关我的 sameElements() 方法的帮助。我需要取两个随机顺序的用户输入数组,看看它们是否具有相同的多重性。

我知道如果它们的长度不同,那么它们就不会有相同的元素。但这就是我所得到的。可以为此修改我的 sameSet() 方法中的代码吗?还是我需要做一些完全不同的事情?

 ` /**
 * checks whether two arrays have the same elements in some order, with
 * the same multiplicities. For example, 1 4 9 16 9 7 4 9 11 and 11 1 4 9 16
 * 9 7 4 9 would be considered identical
 *
 */

 public static boolean sameElements(Integer a[], int sizeA, Integer b[], int sizeB) {
    if (a.length != b.length) {
        return false; 
    }
      // need help on this, length portion is all I have.
 }


 /**
 * 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(Integer[] a, int sizeA, Integer[] b, int sizeB) {

    HashSet<Integer> set1 = new HashSet<Integer>(Arrays.asList(a)); // Put arrays a & b into hashset */
    HashSet<Integer> set2 = new HashSet<Integer>(Arrays.asList(b));

    //Compares the sets to see if they have the same elements 

    if (set1.equals(set2)) {
        System.out.println("The elements of the arrays A and B form the same set.");
    } else {
        System.out.println("The elements of the arrays A and B do not1 form the same set.");
    }
    return set1.equals(set2);

}

public static void main() {
    final int LENGTH = 30;
    int sizeA = 0;
    int sizeB = 0;
    Integer a[] = new Integer[LENGTH];
    Integer b[] = new Integer[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: "); //Gather user input for array A
        while (input.hasNextInt() && sizeA < a.length) {
            a[sizeA] = input.nextInt();
            sizeA++;
        }
        System.out.println("# of values in array A:" + sizeA + "\n");

        System.out.println("Please fill up values for array b, type in Q to quit: "); //Gather user input for array B
        while (in.hasNextInt() && sizeB < b.length) {
            b[sizeB] = in.nextInt();
            sizeB++;
        }
        System.out.println("# of values in array B:" + sizeB);
        //Take the user input and test it through each method
        equals(a, sizeA, b, sizeB); 
        sameSet(a, sizeA, b, sizeB);
        sameElements(a, sizeA, b, sizeB);


    }
}

【问题讨论】:

    标签: java arrays


    【解决方案1】:

    您可以使用 Set 的 containsAll 方法轻松实现。我写了下面的程序。请检查它是否对您有帮助。

        Set a = new HashSet<Integer>();
        Set b = new HashSet<Integer>();
        a.add(1);
        a.add(2);
        a.add(3);
    
        b.add(3);
        b.add(2);
        b.add(1);
        if(a.containsAll(b) && b.containsAll(a))
        {
            System.out.println("Same elements in both");            
        }
        else
        {
            System.out.println("Not same elements in both");
        }
    

    【讨论】:

    • 既然您知道如何将数组转换为集合,我将这部分留在我的代码中
    【解决方案2】:

    您基本上想知道数组是否是彼此的字谜。该问题的一般解决方案是对它们进行排序 (Arrays.sort),然后检查它们是否相等。

    https://stackoverflow.com/a/8103629/1980909

    【讨论】:

    • array.sort 会抛出异常,但如果所有数组值都未填充,不是吗?
    • @Maxwell1222 我怀疑它确实如此,但即使它确实如此,您也可以创建自己的Comparator 而不是,
    • @Maxwell1222 是的。如果可能,您应该使用 int[] 而不是 Integer[]。
    【解决方案3】:

    对不起。我没有很好地阅读这个问题。要执行您想要的操作,您可以按最大和最小对每个数组中的值进行排序,然后检查它们是否相等。

    这里的代码循环遍历数组并检查每个元素与另一个数组的元素。

    boolean equal = true;//If true after for then it is equal if false then it is not equal.
    for(int i = 0; i < a.length; a++){
        if(!a[i].equals(b[i])){
            equal = false;
            break;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-07
      • 1970-01-01
      • 1970-01-01
      • 2011-04-01
      • 2012-08-12
      • 1970-01-01
      • 2021-12-28
      相关资源
      最近更新 更多