增强型for循环
-
在Java中如何使用增强型for循环
增强型for循环用于简化数组和集合的遍历,语法为for(元素类型 变量 : 数组或集合){},可直接访问每个元素。1. 遍历数组时无需索引,如int[] numbers = {1,2,3}; for(int num : numbers) System.out.println(num); 依次输出元素…
增强型for循环用于简化数组和集合的遍历,语法为for(元素类型 变量 : 数组或集合){},可直接访问每个元素。1. 遍历数组时无需索引,如int[] numbers = {1,2,3}; for(int num : numbers) System.out.println(num); 依次输出元素…