By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,654 Members | 1,519 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,654 IT Pros & Developers. It's quick & easy.

How to find the difference between two arrays?

P: 1
Right, so I am trying to find the answer to the above question and then return it as type IntSet. For this question, I need to use arrayList. I have tried tweaking the code a number of times, however, the array contains the wrong elements. The intersection method called works perfectly fine, so the problem is not there. I think it is when I am trying to remove elements from the list that the problems start. Your help is much appreciated.

Expand|Select|Wrap|Line Numbers
  1.         public IntSet difference(Integer[] s1, Integer[] s2) {
  2.  
  3.         IntSet result = intersection(s1, s2);//get intersection array
  4.         IntSet s3;
  5.         Arrays.sort(s1);//sort arrays
  6.         ArrayList<Integer> difference = new ArrayList(Arrays.asList(s1));//creates new array
  7.         int[] s4 = new int[result.al.size()];
  8.         int counter = 0;
  9.         for (int int2 : result.al) {
  10.             s4[counter++] = int2;
  11.         }
  12.         for (int s : s4) {
  13.             if (difference.contains(s)) {
  14.                 difference.remove(s);
  15.             }
Nov 17 '12 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.