1:迭代器遍历模式
Iterator<String> iterator = list.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
2:foreach方式遍历
for (String s :list
) {
System.out.println(s);
}
3:for循环遍历
for (int i = 0; i < list.size(); i++) {
String s = list.get(i);
System.out.println(s);
}
4:综合例题
package com.API_List.List_method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class TestList3 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("未曾清平难成人");
list.add("不仅挫折永天真");
list.add("人情似纸张张薄");
list.add("世事如棋局局新");
list.set(0,"平芜尽处是芳草");
list.remove(0);
// list集合的遍历方式
// 1:迭代器
List<String> newlist = list.subList(1,3);
Iterator<String> iterator = newlist.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
System.out.println("-----------------------------");
// 2:foreach
for (String s :list
) {
System.out.println(s);
}
System.out.println("----------------------------");
// 3:for循环
for (int i = 0; i < list.size(); i++) {
String s = list.get(i);
System.out.println(s);
}
}
}
5:entrySet 遍历Map
Set entrySet = map.entrySet();
Iterator iterator = entrySet.iterator();
while(iterator.hasNext()){
Map.Entry entry = (Map.Entry)iterator.next();
Object key = entry.getKey();
Object value = entry.getValue();
}
Set<Map.Entry<Character, Integer>> entrys = map.entrySet();
for (Map.Entry<Character, Integer> entry :
entrys) {
Character key = entry.getKey();
Integer value = entry.getValue();
}
6:总结
对于底层是数组的接口实现方式:我们可以采用for循环、foreach、迭代器的方式
底层是链表:采用foreach、迭代器的方式
map集合的遍历有以为独特的成员:entrySet 进行遍历
7:建议采纳
如有建议或者错误请私信我进行修改,感谢!