Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
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
Old 11-18-2012, 04:40 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You're over-thinking what you need to do with this.
Since you already have a method to intersect, and you can cast to a list, its simple to remove what is in a collection from another.
PHP Code:
    public static Integer[] difference(Integer[] lhsInteger[] rhs)
    {
        
ArrayList<IntegeralResult = new ArrayList<Integer>(Arrays.asList(lhs));
        
alResult.removeAll(Arrays.asList(intersect(lhsrhs)));
        
        return 
alResult.toArray(new Integer[alResult.size()]);
    } 
That assumes that the intersect's returned IntSet is castable to a list as you have here in your example.
Unless I misunderstand what you are referring to as the difference, that should do what you are needing.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
arraylist, custom types, java

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:58 AM.


Advertisement
Log in to turn off these ads.