package com.Test01; import java.util.ArrayList;import java.util.Iterator; public class ArrayListDemo { public static void main(String[] args) { //创建ArrayList集合对象 ArrayList<Student> array = new ArrayList<Student>(); //创建学生对象 提前定义学生类 Student s1 = new Student("王五",20); Student s2 = new Student("张五",21); Student s3 = new Student("李五",22); //添加学生对象到集合中 array.add(s1);array.add(s2);array.add(s3); //迭代器遍历集合:集合特有的遍历方式 Iterator<Student> it = array.iterator(); while(it.hasNext()) { Student s = it.next(); System.out.println(s.getName()+","+s.getAge()); } System.out.println("------------------------"); //普通for遍历,带有索引 for(int i = 0;i<array.size();i++) { Student s = array.get(i); System.out.println(s.getName()+","+s.getAge()); } System.out.println("-----------------------"); //增强for遍历 for(Student s : array) { System.out.println(s.getName()+","+s.getAge()); } }}
原文地址:https://www.cnblogs.com/lsswudi/p/11407915.html
时间: 2024-10-02 02:58:52