| Author |
Array problem
|
Peter Phung
Ranch Hand
Joined: Dec 06, 2001
Posts: 138
|
|
Can anyone tell me how i get rid of all duplicated values in an array?
|
Pete<br />"Reality is an illusion <br />brought on by a lack of <br />drink, drugs and smut"
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
It ain't pretty... ...and it ain't all that ugly. Good Luck.
|
[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
|
Classes that implement the Set interface do just what you want to do. I used TreeSet here that also sorts the elements of the set. HashSet implements Set and doesn't sort its elements.
|
 |
Peter Phung
Ranch Hand
Joined: Dec 06, 2001
Posts: 138
|
|
|
Does the code work with strings too?
|
 |
Peter den Haan
author
Ranch Hand
Joined: Apr 20, 2000
Posts: 3252
|
|
An array of what exactly, what does "duplicate" mean (equals(), ==), how large do you expect the array to be, can you invalidate individual elements to leave "holes", and how important is performance? Knee-jerk reaction would be to suggest that an array might possibly be an inappropriate data structure for your problem and that you consider replacing it by a Set, probably a HashSet. If you can't replace (or convert) the array with (to) something else, for instance because it's an array of primitives and you can't afford to wrap each element inside an object, there are basically three ways to go about junking duplicates.Sort it (eg Arrays.sort()) so that duplicates occupy consecutive slots, you can then easily invalidate them in a linear scan. Performance is O(n log n).Scan through the array (1..n), comparing each element i to elements i+1 .. n and zapping any duplicates. Simple, but not efficient (O(n^2)).Scan through the array (1..n), putting each element in a hash table as you go along (either a HashSet or a hash structure you write yourself), flagging up any duplicates. Very efficient (O(n)) but eats more memory.If you can't invalidate elements (no holes), you will have to compact your array as you go through it. - Peter
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
Does the code work with strings too?
You're kidding, right? If - you - were - to - try - it - all - by - yourself, you'd find that, yes, it works for all objects that implement Comparable (at least I think that's the stipulation). But, then why would you do it yourself when you have an idiot to do it for you? [ March 13, 2002: Message edited by: Dirk Schreckmann ]
|
 |
Peter den Haan
author
Ranch Hand
Joined: Apr 20, 2000
Posts: 3252
|
|
Hey, those answers weren't there when I started writing A Collection such as a HashSet or a TreeSet will work with any class that fulfills the java.lang.Object contract for equals() and hashCode(). In the case of TreeSet, either the class should implements Comparable or you must supply a suitable Comparator. The java.lang.String class satisfies all requirements. So yes, it will work. - Peter
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
And I was just about to give some pseudo-code similar to what you gave, but far less intelligent sounding and without the efficiency analysis. Let's see, how many times can we trip over each other's threads...
|
 |
Peter Phung
Ranch Hand
Joined: Dec 06, 2001
Posts: 138
|
|
|
can anyone tell me how to output one string at a time if the strings are all being held in a treeset? cos i need to print some values between each of the strings in the treeset.
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
Looking at the documentation for TreeSet in The Java API Specification we see what methods we have available for operating on an instance of the class TreeSet. If you'd rather work with a normal array, you might be interested in the toArray() method, which does just what it sounds like it might do. first() returns the first (lowest) element currently in this sorted set. last() returns the last (highest) element currently in this sorted set. remove(Object o) removes the specified element from this set if it is present. Good Luck. [ March 14, 2002: Message edited by: Dirk Schreckmann ]
|
 |
Peter Phung
Ranch Hand
Joined: Dec 06, 2001
Posts: 138
|
|
if i use the first() method does the first value in the set get erased? i.e. is calling the first() method like placing a pointer at the start of the set ,getting that value, and then moving the pointer onto the next value in the set. the API specification isn't very clear on this.
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
Originally posted by Dirk Schreckmann: If - you - were - to - try - it - all - by - yourself ... But, then why would you do it yourself when you have an idiot to do it for you?
[ March 14, 2002: Message edited by: Dirk Schreckmann ]
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
Perhaps you want to learn about the Stack class. [ March 14, 2002: Message edited by: Dirk Schreckmann ]
|
 |
Peter Phung
Ranch Hand
Joined: Dec 06, 2001
Posts: 138
|
|
Sorry about all the stupid questions, but i don't understand whats going wrong with my code here it is: the problem is when i try to count the no of tokens, the right value is returned but the no of values assigned into the array is 1. Please can u tell me what is going wrong
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
|
Each time through the loop, you're calling nextToken() twice. This cases you to advance through the list of tokens twice as fast as you want. Call it once, and save the value returned in a varaible so you can re-use it.
|
"I'm not back." - Bill Harding, Twister
|
 |
Peter Phung
Ranch Hand
Joined: Dec 06, 2001
Posts: 138
|
|
Thanks for the help so far guys. I appreciate it, but i'm having a bit of a problem with StringTokenizer. here's my code : it is my understanding that the code should work but the variable countParam is 1 when it should be 6. can anyone tell me why this is?
|
 |
Peter Phung
Ranch Hand
Joined: Dec 06, 2001
Posts: 138
|
|
Ignore my last post, the problem was me not using my eyes. Thanks for all the help.
|
 |
 |
|
|
subject: Array problem
|
|
|