Results 1 to 10 of 10
Thread: Removing Duplicates.
- 12-27-2009, 04:18 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
Removing Duplicates.
Hi I'm fairly new to Java and have hit a small problem.
I have a 2D array all ready set up. Now I am searching through that array to populate a JOptionPane for a user to select from.
Ok I can populate the list no problems but what I would like to do is after the String Array has been populated I would like to remove all duplicates first before the list is shown to the user.
Is it possible and how could I achieve this.
Thanks
Code is as follows.
Java Code:String[] code = new String[oceanLink.length]; for (int row = 0; row < oceanLink.length; row++) { code[row] = oceanLink[row][1]; } Object routeCode = JOptionPane.showInputDialog(null, "Please choose a option", "Route Codes", JOptionPane.QUESTION_MESSAGE, null, code, code[1]); System.out.println(routeCode); String port = routeCode.toString(); return port;
- 12-27-2009, 04:24 PM #2
Member
- Join Date
- Jul 2009
- Posts
- 35
- Rep Power
- 0
You could merely use a Set.
Last edited by adz; 12-27-2009 at 04:28 PM.
- 12-27-2009, 04:36 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
Ok I have never used "Set" done a small reading here and there on it but I am unsure where to use it.
Would I use a .add?
a little confussed at the moment.
- 12-27-2009, 05:31 PM #4
yes, an .add(object) is sufficient.
see java.sun.com/javase/6/docs/api/java/util/HashSet.html#add(E) for example
- 12-28-2009, 12:39 AM #5
Member
- Join Date
- Jul 2009
- Posts
- 35
- Rep Power
- 0
Do something like this:
Note: Don't know if this is correct since I made it up or if its even the best way to do it. Considering you're just passing the array back to the method in JOptionPane you can simply use the toArray method actually instead of the array copying crap I posted.Java Code:String[] myArray = {"whatever","whatever","whateverx","whateverx"}; Set<String> noDups = new HashSet<String>(Arrays.asList(myArray)); myArray = new String[noDups.size()]; System.arraycopy(noDups.toArray(), 0, myArray, 0, noDups.size());
Meh I'm tired so I've just re-read what I posted and decided its well, wrong. You could do this:
But er yeah, I'm tired, so apologies if this isn't right too :) Should at least give you the idea, but look up Sets. I suspect if I read this tomorrow I'll wonder why I posted it :/Java Code:Set codeSet = new LinkedHashSet<String>(); for (int row = 0, n = oceanLink.length; row < n; row++) { codeSet.add(oceanLink[row][1]); } Object routeCode = JOptionPane.showInputDialog(null, "Please choose a option", "Route Codes", JOptionPane.QUESTION_MESSAGE, null, codeSet.toArray(), codeSet.get(1)); System.out.println(routeCode); String port = routeCode.toString(); return port;Last edited by adz; 12-28-2009 at 01:12 AM.
- 12-28-2009, 10:25 AM #6
The error in the first example is that although the set contains 2 elements you use arrayCopy method for the two elements. And of course they are equal.
You propably should use the toArray method to achieve desired results
Java Code:String[] myArray = {"whatever","whatever","whateverx","whateverx"}; Set<String> noDups = new HashSet<String>(Arrays.asList(myArray)); myArray = noDups.toArray(new String[noDups.size()]);
- 12-28-2009, 07:04 PM #7
May i interest you in this solution?
Java Code:private static String[] uniqueArguments(String[] arguments) { ArrayList<String> b = new ArrayList<String>(); ArrayList<String> c = new ArrayList<String>(); for (int i = 0; i < arguments.length; i++) { b.add(arguments[i].trim()); } int busted = 0; for (int ix = 0; ix < b.size(); ix++) { busted = 0; for (int j = 0; j < b.size(); j++) { if (b.get(ix).equals(b.get(j))) { busted++; } } if ((busted == 1)) { c.add(b.get(ix)); } else if (busted > 1) { b.set(ix, null); } } // TODO: I don't understand this statement, // why do we need the "(new String[0]) part??? logger.info("Done processing -v list. Looks good ..!"); return c.toArray(new String[0]); } }
- 12-28-2009, 07:25 PM #8
Well this algorithm is slower (quadratic runtime!) than using sets and requires more memory.
To answer your other question ist the "new String[0]" is used to achive additional type cast (see javadoc of toArray method): the method will return an array of String and not an array of Object."There is no foolproof thing; fools are too smart."
"Why can't you solve my Problem ?"
- 12-28-2009, 07:33 PM #9
Oh, and the other thing: if you really need that preprocessing and want to use ArrayList then this should do the same as above
However i'm a fan of HashSets ;-)Java Code:ArrayList<String> c = new ArrayList<String>(); for (String s : arguments) { String myProcessedString = s.trim(); if (!c.contains(myProcessedString)) { c.add(myProcessedString); } } return c.toArray(new String[c.size()]);"There is no foolproof thing; fools are too smart."
"Why can't you solve my Problem ?"
- 12-29-2009, 01:03 PM #10
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Duplicates in more than two sets
By JavaJ in forum New To JavaReplies: 8Last Post: 12-03-2009, 04:07 PM -
No duplicates allowed in Sets
By Java Tip in forum Java TipReplies: 0Last Post: 01-21-2008, 04:33 PM -
removing duplicates from arrays
By bugger in forum New To JavaReplies: 3Last Post: 11-13-2007, 06:11 PM -
Duplicates
By Gambit17 in forum New To JavaReplies: 5Last Post: 11-08-2007, 09:56 AM -
duplicates in iReport
By Heather in forum Advanced JavaReplies: 1Last Post: 07-05-2007, 04:42 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks