package com.jyd.demo;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
*
* 类名: ListDemo
* 备注: list的遍历方式
* 时间: 2017年4月8日 下午4:08:07
* 作者:JYD
* @version
* @since JDK 1.8
*/
public class ListDemo {
public static void main(String[] args) {
Student s1 = new Student(1, "梅超风", "女", 24, 100);
Student s2 = new Student(2, "欧阳锋", "男", 24, 99);
Student s3 = new Student(3, "风清扬", "男", 24, 98);
List<Student> list = new ArrayList<Student>();
list.add(s2);
list.add(s1);
list.add(s3);
/**
* 使用迭代器进行遍历
*/
Iterator<Student> it = list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
System.out.println("=======================");
/**
* 使用循环遍历
*/
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
System.out.println("=======================");
/**
* foreach增强for循环
*/
for (Student student : list) {
System.out.println(student);
}
}
}
List遍历的三种方式
最新推荐文章于 2022-07-24 17:24:06 发布