Vector duplicates
Hello
I'm having a problem with my vector.
When i dubble click in my JTable a TextField is filled in but now i want each time i dubble click the value is put in the Textfield and a Vector.
This works wel but now i have the problem sometimes in the textfield there will be the same value but in de the vector each value must show 1 time.
This is my code for filling the vector and works just fine except the duplicates are also being added. How can i solve my problem?
I think some kind of For lus can do the trick but i don't know how.
publicvoid listDir (){
lijstDir.addElement(tServer.getText());
}
Satanduvel
What about using a [url=http://java.sun.com/j2se/1.4.2/docs/api/java/util/Set.html]Set[/url] instead of a Vector ? A Set is a collection that contains no duplicate elements.
Or you could check that the Vector doesn't contain the element already with the help of its [url=http://java.sun.com/j2se/1.4.2/docs/api/java/util/Vector.html#contains(java.lang.Object)]contains(Object elem)[/url] method.
if you don't want dups in your vector:if (!vec.contains(element))vec.addElement(element);
check out the "contains" method. Read the API for it.
Also (as mentioned in the previous post), you should probably switch to ArrayList and get much better performance, with very little work to make the switch.
Edit: Great, now he edited his post.
I look like an idiot now, as it looks like I simply copied his post, and my reference to his post now makes no sense.
if i understood your problem correctly i guess you can use an if clause for ex:
public void listDir () {
for(int i =0 ;i<listdir.size();i++)
{
String s = lijstDir.get(i).toString();
if(s.equals(Server.getTest()))
{
lijstDir.addElement(tServer.getText());
}
}
i mean if u do not want duplication u should check it
this is same with the first solution by the way
ayberk cansever
Message was edited by:
ayberk>
Check whether the vector has that element before adding it, and add only if doesnt have itif( ! lijstDir.contains(Server.getText() ){lijstDir.addElement(tServer.getText());}
> ...> This is my code for filling the vector and works just> fine except the duplicates are also being added. How> can i solve my problem?By using a java.util.Set: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Set.html
to avoid adding duplicate values, use a map instead:
HashMap:
http://java.sun.com/j2se/1.3/docs/api/java/util/HashMap.html
Hashtable
http://java.sun.com/j2se/1.3/docs/api/java/util/Hashtable.html
Otherwise, before adding an element, check your vector if it already has the element you want to add. Use contains method
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Vector.html#contains(java.lang.Object)
Ok thx 4 the solution it's working.I'm gonna give the first 6 stars and second 4 good?Satanduvel
> to avoid adding duplicate values, use a map instead:> ...If the OP needs to store a key-value pair, yes. But if s/he only needs to store single values, then a Set is more appropriate.
@Calvino_ind: 6 stars@masijade: 4 stars
> Ok thx 4 the solution it's working.> ...If you ended up with a Vector.contains(...) solution, I suggest reading about what a Set does as well. Or at least use an ArrayList instead of a (old and slow) Vector.Good luck.
> @Calvino_ind: 6 stars> @masijade: 4 starseven though Tim and Prometheuzz offered a probably better solution? bit harsh!