LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Vector.contains(Object) (https://www.linuxquestions.org/questions/programming-9/vector-contains-object-658781/)

manolakis 07-28-2008 10:22 AM

Vector.contains(Object)
 
Hi there

I have a class of Person like the following

Quote:

public class Person{

private String surname;
private String name;

public Person(String surname, String name)
{
this.surname = surname;
this.name = name;
}

}
Now what I want to do is to add Persons in a Vector whose surname has not been added before. Therefore the following should result to true:

Quote:

Vector v = new Vector();
v.addElement("Gates","Bill");
boolean b = v.contains("Gates","John"); // Should return true
Any idea how to do that?
Thanks

ntubski 07-28-2008 11:42 AM

From http://java.sun.com/j2se/1.4.2/docs/....lang.Object):
Quote:

public boolean contains(Object elem)

Tests if the specified object is a component in this vector.
...
Returns:
true if and only if the specified object is the same as a component in this vector, as determined by the equals method; false otherwise.
So you should override the equals method for your Person class, so that it returns true for Person's with the same surname.

jiml8 07-28-2008 12:05 PM

Quote:

Originally Posted by manolakis (Post 3228751)
Hi there

I have a class of Person like the following



Now what I want to do is to add Persons in a Vector whose surname has not been added before. Therefore the following should result to true:



Any idea how to do that?
Thanks

I am not a java programmer.

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.

ntubski 07-28-2008 12:53 PM

Quote:

Originally Posted by jiml8 (Post 3228849)
...
really be this:

Vector v = new Person();
...

That wouldn't work unless Person extends Vector, probably the OP meant
Code:

Vector v = new Vector();
v.addElement(new Person("Gates","Bill"));
boolean b = v.contains(new Person("Gates","John")); // Should return true


jiml8 07-28-2008 01:06 PM

Yes, OK. That makes sense.

Nylex 07-28-2008 01:18 PM

You can also make a Vector for Person objects only with the following:

Vector<Person> v = new Vector<Person>();

paulsm4 07-28-2008 02:07 PM

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.