Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Finding the difference of two arrays using arrayList

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 11-17-2012, 03: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 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 difference = new ArrayList(Arrays.asList(s1));//creates new array ArrayList 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 am) { if (!am.contains(elem)) { return NUMBER_NOT_IN_SET; } am.remove(elem); return NUMBER_REMOVED; }``` Last edited by keithf; 11-17-2012 at 05:40 PM..
 11-18-2012, 05:40 PM PM User | #2 Fou-Lu God Emperor     Join Date: Sep 2002 Location: Saskatoon, Saskatchewan Posts: 16,605 Thanks: 4 Thanked 2,598 Times in 2,567 Posts 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[] lhs, Integer[] rhs)    {        ArrayList alResult = new ArrayList(Arrays.asList(lhs));        alResult.removeAll(Arrays.asList(intersect(lhs, rhs)));                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.

 Bookmarks

 Tags arraylist, custom types, java

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 01:39 PM.