LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-28-2008, 10:22 AM   #1
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Rep: Reputation: 37
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
 
Old 07-28-2008, 11:42 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
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.
 
Old 07-28-2008, 12:05 PM   #3
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by manolakis View Post
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.
 
Old 07-28-2008, 12:53 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by jiml8 View Post
...
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
 
Old 07-28-2008, 01:06 PM   #5
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
Yes, OK. That makes sense.
 
Old 07-28-2008, 01:18 PM   #6
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

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

Vector<Person> v = new Vector<Person>();
 
Old 07-28-2008, 02:07 PM   #7
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Allthough squid has 1 object, it still asks object to http server markraem Linux - Server 0 10-08-2007 11:40 AM
LXer: Vector Linux 5.9-Pseudo64-0.1 -- Finally, 64-bit Vector LXer Syndicated Linux News 0 09-12-2007 01:20 PM
can i modify int 80 vector to a user-defined vector qqrilxk Linux - Security 1 03-03-2005 08:46 PM
Miseries at install from HDD&floppies, with vector ISO& how to boot with vector linux prctom VectorLinux 9 06-29-2004 05:27 AM
Event driven object-to-object: C++ template class mecanism ( NOT STL or STDC++) bretzeltux Programming 2 12-23-2003 02:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration