- import java.util.ArrayList;
- import java.util.ListIterator;
- import com.baidu.Student;
- public class Test4 {
- public static void main(String[] args) {
- ArrayList arrayList = new ArrayList();
- arrayList.add(new Student("zhangsan", 18));// 添加元素
- arrayList.add(new Student("zhangsan", 16));// 添加元素
- arrayList.add(new Student("zhangsan", 15));// 添加元素
- // 获取迭代器对象
- ListIterator listIterator = arrayList.listIterator();
-
- //普通for循环遍历集合方式
- for (int x = 0; x < arrayList.size(); x++) {
- Student s = (Student) arrayList.get(x);// 向下转型
- System.out.println(s.getName() + " " + s.getAge());
- }
- /*增强for遍历集合方式
- * for (Object o : arrayList) {
- * Student s = (Student) o;// 向下转型
- * System.out.println(s.getName() + " " + s.getAge());
- * }
- */
-
- /*迭代器的方式
- * while (listIterator.hasNext()) {
- * Student s = (Student)listIterator.next();// 向下转型
- * System.out.println(s.getName() + " " +s.getAge());
- * }
- */
- }
- }
复制代码
|
|