1. 小案例
/**
* 练习 集合存储对象进行遍历
* foreach 增强 for遍历
* Iterator 迭代器遍历
*/
public class CollectionDemo {
public static void main(String[] args) {
Collection<Student> c = new ArrayList<Student>();
c.add(new Student("小明", 23, 175));
c.add(new Student("小莉", 20, 170));
c.add(new Student("小陌", 21, 165));
System.out.println(c); //默认是打出集合的地址, 但是存储的对象重写了toString方法
//增强 for 进行遍历集合元素
for (Student s : c) {
System.out.println("名字" + s.getName() + "年龄" + s.getAge() + "身高" + s.getHeight());
}
//迭代器 iterator遍历集合元素的Student对象
final Iterator<Student> iterator = c.iterator();
while (iterator.hasNext()) {
Student s = iterator.next();
System.out.println("名字" + s.getName() + "年龄" + s.getAge() +