View Single Post
Old 11-17-2012, 02:08 PM   PM User | #1
keithf
New to the CF scene

 
Join Date: Nov 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
keithf is an unknown quantity at this point
Post Finding the difference of two arrays using arrayList

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. UPDATE: I have now managed to return an array, however all the remove element method does is take away 3 from the array.

Code:
public IntSet difference(Integer[] s1, Integer[] s2) {

        IntSet result = intersection(s1, s2);//get intersection array
        IntSet s3;
        Arrays.sort(s1);//sort arrays
        ArrayList<Integer> difference = new ArrayList(Arrays.asList(s1));//creates new array
        ArrayList<Integer> outcome = new ArrayList(Arrays.asList(result));
        int[] s4 = new int[result.al.size()];
        int counter = 0;
        for (int int2 : result.al) {
            s4[counter++] = int2;
        }
        int count2 = outcome.size();
        for (int i = 0; i < count2; i++) {
            if (difference.contains(s4[i])!= false) {
                removeElement(s4[i],difference);
                count2--;
            }
            
        }
        s3 = new IntSet(difference);
            return s3;
    }

    public int removeElement(int elem, ArrayList<Integer> am) {

        if (!am.contains(elem)) {
            return NUMBER_NOT_IN_SET;

        }
            am.remove(elem);
            return NUMBER_REMOVED;
        }

Last edited by keithf; 11-17-2012 at 04:40 PM..
keithf is offline   Reply With Quote