package com.neuedu.list;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
public class ListOutputDemo02 {
/**
* 五种输出ArrayList的方法
* 1、for
* 2、foreach
* 3、toString
* 4、Iteator
* 5、ListIterator
*/
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
list.add("5");
//1\
for(int i = 0 ; i<list.size();i++){
System.out.print(list.get(i));
}
System.out.println("\n----------------------");
//2\
for (Object object : list) {
System.out.print(object);
}
System.out.println("\n----------------------");
//3\
System.out.println(list);//默认调用toString
System.out.println("\n----------------------");
//4、
Iterator i = list.iterator();
while(i.hasNext()){
System.out.print(i.next());
}
System.out.println("\n----------------------");
//5.1\正着输出
ListIterator li = list.listIterator();
while(li.hasNext()){
System.out.print(li.next());
}
System.out.println("\n----------------------");
//5.2\反向输出
while(li.hasPrevious()){
System.out.print(li.previous());
}
System.out.println("\n----------------------");
}
}
---------------------
作者:geng_027
来源:CSDN
原文:https://blog.csdn.net/weixin_35221883/article/details/79289864
版权声明:本文为博主原创文章,转载请附上博文链接!