本帖最后由 吴愿涛 于 2012-12-7 22:13 编辑
- package com.itheima.bbs.exercise;
- 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 String getName() {
- return name;
- }
- public int getAge() {
- return age;
- }
-
- }
- class ArrayListTest {
- /**
- * @param args
- */
- public static void sop(Object obj){
- System.out.println(obj);
- }
- public static void main(String[] args) {
- ArrayList al = new ArrayList();
- al.add(new Person("a",1));
- al.add(new Person("b",2));
- al.add(new Person("c",3));
- al.add(new Person("d",4));
- al.add(new Person("e",5));
- al.add(new Person("f",6));
-
- Iterator<Person> it = al.iterator();
-
- while(it.hasNext()){
- Person p = it.next();
- String name = p.getName();
- int age = p.getAge();
-
- sop(name+":"+age);
- }
- }
- }
复制代码 |