1.vector等集合类的contains函数使用类的equals函数来比较
例如定义类:
public class People {
public int age = 0;
@Override
public boolean equals(Object arg0) {
if (!(arg0 instanceof People)) {
return false;
}
People people = (People) arg0;
if (people.age == this.age) {
return true;
}
return false;
}
}
使用
public static void main(String[] args)
{
try {
People aPeople = new People();
aPeople.age = 20;
Vector<People> peoples = new Vector<People>();
peoples.add(aPeople);
People bPeople = new People();
bPeople.age = 20;
System.out.println(peoples.contains(aPeople));///打印true
System.out.println(peoples.contains(bPeople));///打印true
} catch (Exception e) {
e.printStackTrace();
}
}