public class ThreadTest5 {
public static void main(String[] args) {
List list=new ArrayList();
list.add(new Person("张三",23));
list.add(new Person("李四",35));
list.add(new Person("王五",45));
list.add(new Person("张三",23));
list.remove(3);
for(Object obj:list){
Person p=(Person)obj;//你这里应该用obj,是你的高级for循环用错对象了
System.out.println(p);
}
}
}
class Person{
private String name;
private int age;
public Person(String name,int age){
this.name=name;
this.age=age;
}
public String toString(){
return this.name+"------"+this.age;
}
} |