当前位置: 代码迷 >> J2SE >> 会合框架练习
  详细解决方案

会合框架练习

热度:36   发布时间:2016-04-23 20:32:55.0
集合框架练习
import java.util.*;
class student{
private String name;
private int age;
student(String name,int age){
this.name=name;
this.age=age;
}
public boolean equals(Object obj){
if(!(obj instanceof student)){
return false;
}
student e=(student)obj;
return this.name.equals(e)&&this.age==e.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)){
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);
}
}
这个代码是要取不同的学生,但是我运行出来的结果是相同的不同的都取出来了。这是什么 问题  想不通。  求指导谢谢!
------解决方案--------------------
return this.name.equals(e)&&this.age==e.age;
  相关解决方案