首页 > 代码库 > List的遍历
List的遍历
package ListPackage;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class Demo3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//遍历输出list
List<Integer> list = new LinkedList<Integer>();// 链表
list.add(78);
list.add(89);
list.add(1);
list.add(2);
list.add(45);
list.add(67);
List<Integer> list1 = new ArrayList<Integer>();
list1.addAll(list); //添加已有链表
//for-each遍历
for(Integer s : list){
System.out.print(s+" ");
}
System.out.println();
//size遍历
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i)+" ");
}
System.out.println();
//迭代器iterator
Iterator<Integer> a = list.iterator();
while(a.hasNext()){
System.out.print(a.next()+" ");
}
}
}
List的遍历