总结了一下List集合遍历的几种方法供大家参考:
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
//方法一
Object[] arr = list.toArray();
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
//方法二
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
//方法三
Iterator<String> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
//方法四
ListIterator<String> lit = list.listIterator();
while (lit.hasNext()) {
System.out.println(lit.next());
}
|
|