四种循环遍历方法
public class circulationMethod {
public static void main(String[] args) {
//获得一个集合pets,可以直接用List。of();
// List<String> list = new ArrayList<>();
List<String> pets = List.of("z", "p", "c", "10", "21");
//[1]转换为数组再循环遍历
Object[] obj = pets.toArray();
for (Object o : obj) {
System.out.print(o);
}
System.out.println();
//[2]普通for循环
for (int i = 0; i < pets.size(); i++) {
System.out.print(pets.get(i));
}
System.out.println();
//[3]迭代器(快捷键itit回车)
Iterator<String> it = pets.iterator();
while (it.hasNext()) {
String next = it.next();
System.out.print(next);
}
System.out.println();
//[4]增强for
for (String pet : pets) {
System.out.print(pet);
}
}
}