-
ArrayList、linkedList、Vector三种集合遍历
-
方式一:iterator迭代器遍历list
//iterator迭代器遍历 Iterator iterator = list.iterator(); //快速生成快捷键 itit while (iterator.hasNext()) { //判断迭代器是否为空 Object next = iterator.next(); //指针,获取数据 System.out.print(next + " "); }
-
方式二:增强for循环遍历
//增强for循环遍历list 快速生成快捷键 I for (Object o : list) { System.out.print(list); }
-
方式三:for循环遍历
//for循环遍历list 快速生成快捷键 fori for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i) + " "); }
-
-
实例:
-
import java.util.*; public class ListTraverse { public static void main(String[] args) { //创建对象 // ArrayList list = new ArrayList(); // linkedList list = new linkedList(); Vector list = new Vector(); //添加数据 list.add("Jack"); list.add("Tom"); list.add("CiCi"); //iterator迭代器遍历list Iterator iterator = list.iterator(); //快速生成快捷键 itit while (iterator.hasNext()) { //判断迭代器是否为空 Object next = iterator.next(); //指针,获取数据 System.out.print(next + " "); } System.out.println(); //增强for循环遍历list 快速生成快捷键 I for (Object o : list) { System.out.print(list); } System.out.println(); //for循环遍历list 快速生成快捷键 fori for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i) + " "); } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)