Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

duplicates  RSS feed

 
Mario Bonassin
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I've exhausted everything I could think of to do this simple thing. I want to get ride of all the duplicate entries in one list. but every way I try it I get more duplicate entries.
heres the most recent version of what I'm trying to do

any suggestions??
Thanks
Mario
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, just to check...you're comparing equality like this:

if (listModel.getElementAt(a) != doubleModel.getElementAt(i)) {

Have you considered whether you should be using Element.equals() rather than != or ==?
That said, it looks like you have a flaw in your logic. For every element in listModel, you compare to every element in doubleModel, and if they're not equal, you add the former.
In a situation like this it can be helpful to define a "high level" algorithm first. I'd use something like this:

Now, each line can be translated into Java pretty readily. You'll make your life a lot easier if you add a "hasElement()" method that returns a Boolean, you can use this in the if statement in line three. It also avoids the having a nested loop which makes the code much simpler.
Hope that all helps, and post back if you need more tips.
Cheers,

--Tim
 
Mario Bonassin
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot, that has worked beautifully.
Mario
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!