Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

prevent duplicate items

Status
Not open for further replies.

Cathy105

Programmer
Joined
Jul 7, 2002
Messages
12
Location
US
I'm extremely new to Java. I have set up a database using a vector. Everything runs fine. When I select an item from my database, it goes to a table in my program. However, I can select the item a million times and it will show up a million times.

How can I indicate that I have added a duplicate item to the list and keep the user from being able to select the item again? If anyone has any ideas...or can tell me where I can find information.


void cmdRequest_actionPerformed(ActionEvent e) {
System.out.println("hit the add to list button");
DogObject dg = (DogObject)vDog.elementAt(iTrack);
vSelect.addElement(dg);
fillCombo();
}

 
If you don't want duplicates, use a Set instead of a List/Vector. A Set disallows duplicates.

If you are adding user defined types to a set (e.g. your DogObject) then you will have to implement some additional methods/interfaces such as equals(), Comparable and hashCode (depending on which type of Set implementation you use) in order for this to work.
 
Appreciate your help. Unfortunately, I am still quite new to this. I tried using equals earlier today, but don't know what I'm doing. I instatiated 2 new DogObjects and tried comparing them in a For loop and if statement using equals but I could not get it to work. I am unfamiliar with hash tables. Can you recommend a good learning site? If you happen to have an code that uses equals and hash tables together that would also be great.

Happy New Year
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top