java中List 遍历
代码编写
package com.xiang;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: xiang
* Date: 2021/11/13 20:12
*/
public class ListTraverse {
/**
* list 遍历
*
* @param args
*/
public static void main(String[] args) {
List<String> list = Arrays.asList("tom", "cat", "Jane", "jerry");
System.out.println("方法1 集合类的通用遍历方式");
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next() + ",");
}
System.out.println();
System.out.println("方法2 集合类的通用遍历方式");
for (
Iterator<String> iterator2 = list.iterator();
iterator2.hasNext();
) {
System.out.print(iterator2.next() + ",");
}
System.out.println();
System.out.println("方法3 增强型for循环遍历");
for (String value : list) {
System.out.print(value + ",");
}
System.out.println();
System.out.println("方法4 一般型for循环遍历");
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + ",");
}
}
}
运行结果
方法1 集合类的通用遍历方式
tom,cat,Jane,jerry,
方法2 集合类的通用遍历方式
tom,cat,Jane,jerry,
方法3 增强型for循环遍历
tom,cat,Jane,jerry,
方法4 一般型for循环遍历
tom,cat,Jane,jerry,
Process finished with exit code 0