for (元素类型 变量名 : 数组或集合) {
System.out.println(变量名);
}
2、缺点:没有下标
public class Demo{
public static void main(String[] args) {
int[] arr = {1,2,3,4,5};
//普通for循环
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
public class Demo{
public static void main(String[] args) {
int[] arr = {1,2,3,4,5};
//增强for循环
for (int value : arr) {
System.out.println(value);
}
}
}
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Demo{
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("qw");
list.add("er");
Iterator<String> it = list.iterator();
for (String s : list) {
System.out.println(s);
}
}
}
原文:https://www.cnblogs.com/yu011/p/12819455.html