标签:
public class Person { private String name;//,sex; private int age; public Person(String name, int age) { super(); 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; } }
定义一个Person类,包含姓名年龄性别身高体重等数据,并提供相应的get和set方法。
import java.util.ArrayList; import java.util.Iterator; public class Test { public static void main(String[] args) { Person pA = new Person("A", 20); Person pB = new Person("B", 21); Person pC = new Person("C", 22); Person pD = new Person("D", 24); ArrayList<Person> al = new ArrayList<Person>(); al.add(pA); al.add(pB); al.add(pC); al.add(pD); for (Iterator<Person> iterator = al.iterator(); iterator.hasNext();) //迭代器 { Person p = (Person) iterator.next(); //转换成person类 System.out.println(p.getName()+" "+p.getAge()); } } }
标签:
原文地址:http://www.cnblogs.com/qq28902581/p/4499500.html