Vector.contains(Object)
Hi there
I have a class of Person like the following Quote:
Quote:
Thanks |
From http://java.sun.com/j2se/1.4.2/docs/....lang.Object):
Quote:
|
Quote:
But I read this code and it looks very wrong to me. Should this: Vector v = new Vector(); really be this: Vector v = new Person(); ? If so, it makes sense to me. If not, it doesn't. |
Quote:
Code:
Vector v = new Vector(); |
Yes, OK. That makes sense.
|
You can also make a Vector for Person objects only with the following:
Vector<Person> v = new Vector<Person>(); |
Manolakis -
A couple of points: 1. I assume your original question was answered: you can easily use the "contains()" method to see if a duplicate element exists 2. Nylex's point is very well taken - you should definitely use the generic version of "Vector" if/whenever possible. 3. Be aware that Vector[] suffers a slight performance hit, because it's "synchronized". If threading isn't an issue, you might prefer using "ArrayList" instead of "Vector". 4. Finally, the Java "Set" collection prohibits duplicates. In your application, "Set" might actually be a better choice than array, Vector or ArrayList. IMHO .. PSM |
All times are GMT -5. The time now is 02:19 PM. |