集合框架—ArrayList的用法

Person.java
package p.bean;

public class Person {
    
    private String name;
    private int age;
    
    public Person() {
        super();
        // TODO Auto-generated constructor stub
    }
    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;
    }
    
    

}


ArrayListTest.java

package arraylist.test;

import java.util.ArrayList;
import java.util.Iterator;

import p.bean.Person;

public class ArrayListTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        
        
        ArrayList al=new ArrayList();
        al.add(new Person("李四1",21));
        al.add(new Person("李四2",22));
        al.add(new Person("李四3",23));
        al.add(new Person("李四4",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());
        }
        

    }

}
如果要用迭代器取数据,只能在循环里面写一个next()方法,因为该方法会自动往下走。还有就是在取自定义对象时,要注意强转。
posted @ 2014-04-12 11:11  .stone_  阅读(134)  评论(0编辑  收藏  举报