【发布时间】:2021-09-04 03:14:12
【问题描述】:
如何使用 for-each 循环结构创建一个包含用户输入的数组?
我通常会这样做:
for (int i = 0; i < array.length; i++) {
System.out.println("Include a number into the array:");
array[i] = sc.nextInt();
}
我怎样才能做同样的事情,但使用 for-each 样式循环?
【问题讨论】:
-
反问:我们为什么要使用
foreach-loop?我们丢失了索引信息,显然我们需要访问特定的数组元素。 -
hi @AdrianMantilla - for (T curr : collection) - 当你已经收集了集合的内容时..