- package com.kxg_Collection;
- //遍历集合中的元素
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Iterator;
- public class CollectionDemo3 {
- public static void main(String[] args) {
- Collection c = new ArrayList();
- Person p = new Person("李延旭", 20);
- Person p2 = new Person("任兴亚", 23);
- Person p3 = new Person("赵磊", 19);
- Person p4 = new Person("王澳", 20);
- Person p5 = new Person("康小广", 23);
- c.add(p);
- c.add(p2);
- c.add(p3);
- c.add(p4);
- c.add(p5);
- // Iterator iterator():迭代器,集合的专用遍历方式
- Iterator i = c.iterator();
- while (i.hasNext()) {
- System.out.println(i.next());
- }
- }
- }
复制代码 |