import java.util.*;
class student{
private String name;
private int age;
student(String name,int age){
this.name=name;
this.age=age;
}
public String toString(){
return this.name+this.age;
}
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
}
class Test{
public static ArrayList getXt(ArrayList w){
ArrayList b=new ArrayList();
Iterator it=w.iterator();
while(it.hasNext()){
student kl=(student)it.next();
if(!b.contains(kl.getName())){
b.add(kl);
}
}
return b;
}
public static void main(String[] args){
ArrayList a=new ArrayList();
a.add(new student("张三",12));
a.add(new student("李艾",15));
a.add(new student("张三",12));
a.add(new student("李艾",12));
a.add(new student("张三",15));
out(getXt(a));
}
public static void out(Object obj){
System.out.println(obj);
}
}
按我的理解是应该输出"张三",12,李艾",15这两个不同姓名的学生对象吧 getName输出的是字符串 String的equals自己不是重写过了么 。。那为什么我这还是全部输出。。求指导谢谢 (迷惑了好久)
------解决方案--------------------
b.add(kl);
kl = student
kl.getName() 是 string name
b.contains(kl.getName())
b 里面是student kl.getName() 是student的name