本帖最后由 木华 于 2014-6-23 21:25 编辑
import java.util.ArrayList;
import java.util.Iterator;
class Person
{
private String name;
private int age;
public Person( String name ,int age)
{
this.name=name;
this.age=age;
}
public int getAge()
{
return age;
}
public String getName()
{
return name;
}
}
class ArrayListTest
{
public static void main(String[] args)
{
Person p1 = new Person("lisi1",21);
ArrayList al = new ArrayList();
al.add(p1);
al.add(new Person("lisi2",22));
al.add(new Person("lisi3",23));
al.add(new Person("lisi4",24));
Iterator it = al.iterator();
while(it.hasNext())
{
System.out.println(((Person)it.next()).getName()+"::"((Person)it.next()).getAge()));
Person p = (Person)it.next();
System.out.println(p.getName()+"--"p.getAge());
}
}
}
|
|