Java集合的遍历方式有以下几种:
List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String element = iterator.next();
// 处理element
}
List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
for (String element : list) {
// 处理element
}
List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.stream().forEach(element -> {
// 处理element
});
List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.forEach(element -> {
// 处理element
});
List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
ListIterator<String> listIterator = list.listIterator();
while (listIterator.hasNext()) {
String element = listIterator.next();
// 处理element
}