import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class ThreePrint {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
//普通for循环,通过.get()来获取每个位置的下标
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i)+" ");
}
//增强for循环,s为变量,直接打印即可
for (String s : list) {
System.out.print(s+" ");
}
//Iterator ,单向迭代器 it.hasNext()指向下一个指针,it.next()指向下一个数
Iterator<String> it = list.iterator();
while (it.hasNext()) {
String s = it.next();
System.out.print(s+" ");
}
//继承了Iterator迭代器,功能更加强大,是双向的,可以逆序打印。
正序打印集合
ListIterator<String> lit = list.listIterator();
while (lit.hasNext()) {
String s = lit.next();
System.out.print(s+" ");
}
//逆序打印集合
while (lit.hasPrevious()) {
String s = lit.previous();
System.out.print(s+" ");
}
}
}