比较两个数组的值是否存在

时间:2014-11-11 04:39:11

标签: java arrays oop

我有这个代码来检查两个数组并打印出另一个数组中不存在的值。我认为我这样做的方式并不是最有效的方法,因此有人能提供更好的OOP方式来用Java编写这些代码吗?

谢谢

 public class Calculate {
        static int [] x = {1,2,4,6,7};
        static int [] y = {2,3,4,6,7}; 
        static boolean xflag = true;
        static boolean yflag = true;

        public static void main(String[] args) {
            // TODO Auto-generated method stub

            for(int i = 0; i<x.length; i++)
            {

            for (int b=0;  b<y.length; b++)
                {
                    if(x[i]!= y[b])
                    {
                        xflag= false;

                    }
                    else
                    {
                        xflag = true;
                        break;
                    }

                }

                if(xflag==false)
                {
                    System.out.println(x[i] +" does not exist in array 2");
                }

            }


            for(int i = 0; i<x.length; i++)
            {

            for (int b=0;  b<y.length; b++)
                {
                    if(y[i]!= x[b])
                    {
                        yflag= false;

                    }
                    else
                    {
                        yflag = true;
                        break;
                    }

                }

                if(yflag==false)
                {
                    System.out.println(y[i] +" does not exist in array1");
                }

            }





        }


    }

5 个答案:

答案 0 :(得分:2)

使用Collection类removeAll方法

  String original[] = { "1","2","3","4","6"};
     String testStr[] = { "1","2","3","5","7" };
        List origList = new ArrayList(Arrays.asList(original));
        List testList = new ArrayList(Arrays.asList(testStr));

        System.out.println(origList.removeAll(testList));
        System.out.println(origList);

答案 1 :(得分:1)

你可以使用java集合框架,有很多功能, 这是一个简单的例子检查它。

public static void main(String a[]){

    List<String> sl = new ArrayList<String>();
    sl.add("apple");
    sl.add("java");
    sl.add("c++");
    sl.add("unix");
    sl.add("orange");
    sl.add("airtel");
    List<String> tl = new ArrayList<String>();
    tl.add("job");
    tl.add("oracle");
    tl.add("jungle");
    tl.add("cricket");
    boolean isCommon = Collections.disjoint(sl,tl);
    System.out.println("Does not found any common elements? "+isCommon);
    tl.add("java");
    isCommon = Collections.disjoint(sl,tl);
    System.out.println("Does not found any common elements? "+isCommon);
}

答案 2 :(得分:1)

如果您想要从实现逻辑中抽象出来,可以使用Apache的CollectionUtils来实现此目的。例如:

public static void main(String[] args) {
        List<Integer> list1=Arrays.asList(1,2,4,6,7);
        List<Integer> list2=Arrays.asList(2,3,4,6,7);

    System.out.println(CollectionUtils.disjunction(list1,list2));
    }

答案 3 :(得分:1)

您可以这样编码

List<Integer> array1 = Arrays.asList(1,2,4,6,7);
List<Integer> array2 = Arrays.asList(2,3,4,6,7);

List<Integer> disjointArray = new ArrayList<Integer>();
for (Integer value : array1) {
    if (!array2.contains(value)) {
        disjointArray.add(value);
    }
}

然后你可以打印disjointArray或做你想做的任何操作。

答案 4 :(得分:0)

这是一个使用Javas Collection类的运行示例:

public class Disjunction {
    public static void main(String args[]) throws UnsupportedEncodingException {
        //Some data preparation
        List<Integer> list1=Arrays.asList(1,2,4);
        List<Integer> list2=Arrays.asList(5,2,8);

        //Here calculating data1-data2 and data2-data1, collect all list items
        //that are in data1 or in data2 but not in both.
        List<Integer> data1 = new ArrayList<>(list1);
        data1.removeAll(list2);
        List<Integer> data2 = new ArrayList<>(list2);
        data2.removeAll(list1);

        //Merging both results. data1 contains now exclusive or of list1 and list2
        data1.addAll(data2);

        System.out.println("exclusive or is " + data1);
    }
}

打印出来

exclusive or is [1, 4, 5, 8]