public class Text1 {
public static void main(String[] args){
List<Person> list = new ArrayList<Person>();
list.add(new Person("zhangsan",34));
list.add(new Person("zhaan",36));
list.add(new Person("zhadfan",24));
list.add(new Person("zhang",56));
Iterator<Person> it = list.iterator();
while(it.hasNext()){
//System.out.println((Person)it.next().getName);//为什么这么写不行啊
Person p =it.next();
System.out.println(p.getName()+"....."+p.getAge());
}
}
}
class Person{
private String name;
private int age;
Person(String name,int age){
this.name = name;
this.age = age;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
} |