3

我有两个可能有重复的数组。我需要将它们作为集合进行比较。

例如{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;
 }
}
4

6 回答 6

5

TreeSet 是一个排序集,因此它将免费进行排序和重复删除。因此,您所要做的就是将数组加载到其中,然后使用 .equals()。

Integer[] value1 = { 11, 7, 9, 16, 4, 1 };
Integer[] value2 = { 11, 11, 7, 9, 16, 4, 1 };

Set<Integer> tSet1 = new TreeSet<Integer>(Arrays.asList(value1));
Set<Integer> tSet2 = new TreeSet<Integer>(Arrays.asList(value2));

System.out.println(tSet1);
System.out.println(tSet2);

System.out.println(tSet1.equals(tSet2));

输出

[1, 4, 7, 9, 11, 16]
[1, 4, 7, 9, 11, 16]
true
于 2012-09-21T06:25:20.827 回答
1
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.HashSet;
import java.util.Set;


public class Test {
    public static class SameElement {
        /**
         * Constructor.
         */
        private SameElement()
        {
            // avoid instatiation
        }

        /**
         * Check if the provided 2 arrays have the same elements ignoring order and duplicates
         * 
         * @param val1 1st array
         * @param val2 2nd array
         * @return true if so.
         */
        public static boolean sameSet(int[] val1, int[] val2)
        {
            return toSet(val1).equals(toSet(val2));
        }

        /**
         * Transform provided array of int into a {@link Set} of {@link Integer}.
         * 
         * @param vals Array of int to use
         * @return a {@link Set} of {@link Integer} (empty if vals is null)
         */
        private static Set<Integer> toSet(int[] vals)
        {
            final Set<Integer> set = new HashSet<Integer>();
            if (vals != null) {
                for (final int i : vals) {
                    set.add(i);
                }
            }
            return set;
        }
    }

    @org.junit.Test
    public void testSameSet()
    {
        int[] value1 = { 11, 7, 9, 16, 4, 1 };
        int[] value2 = { 11, 11, 7, 9, 16, 4, 1 };
        int[] value3 = { 8, 11, 11, 7, 9, 16, 4, 1 };
        assertTrue(SameElement.sameSet(value1, value2));
        assertFalse(SameElement.sameSet(value3, value1));
        assertFalse(SameElement.sameSet(value3, value2));
        assertFalse(SameElement.sameSet(null, value2));
        assertFalse(SameElement.sameSet(value1, null));
        assertTrue(SameElement.sameSet(null, null)); // check against your requirements
    }
}
于 2012-09-21T06:10:34.237 回答
1

用于Arrays.sort对数组进行排序。

于 2012-09-21T05:52:26.450 回答
0

我已经修改了您的sameSet方法,它不需要对您的数组进行排序并处理重复项而不删除它们。

public static boolean sameSet(int[] a, int[] b)
{
    for (int i = 0; i < a.length; i++)
    {
        boolean match = false;
        for (int j = 0; j < b.length; j++)
        {
            if (a[i] == b[j])
            {
                match = true;
                break;
            }
        }
        if (!match)
            return false;
    }
    return true;
}

我有很大的优化空间,但目前可以达到目的。

于 2012-09-21T06:13:06.383 回答
0

下面是我附带的代码。

  1. 检查数组的长度以确定哪个最大。
  2. 外循环使用最大数组
  3. 对于最大数组中的每个元素迭代较小的数组
  4. 检查每个元素,如果未找到,则声明为不相等。

这是代码。

public static void main(String[] args) {

    int[] value1 = { 11, 7, 9, 16, 4, 1 };
    int[] value2 = { 11, 11, 7, 9, 16, 4, 1 };

    int[] firstArray = null;
    int[] secondArray = null;
    //Max Length Array should be used for outer loop
    if(value2.length >value1.length)
    {
        firstArray = value2;
        secondArray = value1;
    }
    else
    {
        firstArray = value1;
        secondArray = value2;
    }
    boolean equal = true;
    for (int i = 0; i < firstArray.length; i++) {
        boolean found = false;//each iteration initialise found to false
        for (int j = 0; j < secondArray.length; j++) {
            if (firstArray[i] == secondArray[j]) {
                found = true;
                break;// as there is no point running the loop
            }

        }
        if (!found) {
            equal = false;//check after each iteration if found is false if false arrays are not equal and break
            break;
        }

    }
    System.out.println(equal);


}
于 2012-09-21T07:03:52.893 回答
0
        int[] value1 = {11, 7, 9, 16, 4, 1};
        int[] value2 = {11, 11, 7, 9, 16, 4, 1};
        HashSet<Integer> set = new HashSet<Integer>();
        HashSet<Integer> set2 = new HashSet<Integer>();

           for(int i=0; i<value1.length;i++) {
                  set.add(value1[i]);
           }
        for(int j=0; j<value2.length; j++) {
                 set2.add(value2[j]);
           }
          now do the sorting and compare both the sets
于 2012-09-21T05:55:42.240 回答