有一个person类:package com.Collection.ArrayList;
public class Person {
private String name;
private int age;
public Person(){}
public Person(String name,int age){this.name=name;this.age=age;}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
一个ArrayTest类如下,
package com.Collection.ArrayList;
import java.util.*;
public class ArrayDemo {
public static void main(String[] args) {
demo();
}
public static void demo()
{
ArrayList al = new ArrayList<Person>();
al.add(new Person("张三",55));
al.add(new Person("李四",15));
al.add(new Person("王武",65));
al.add(new Person("王武",65));
Iterator<Person> it =al.iterator();
while(it.hasNext())
{
System.out.println(it.next().getName()+it.next().getAge());
}
}
}
为什么遍历结果只是输出啦:张三15 王武65两个人,老毕讲过,忘记啦! |