java - 对于每个循环 : can we populate an array?

标签 java

我可以使用 for each 语法从数组中读取数据:

int[] a = new int[100];
for (int i = 0; i < 100; i++){
    a[i] = i;
}


for (int element : a){
    System.out.println(element);                         
}

但是否可以同样填充数组。比如说,使用 i*2 值?
我没有发明这样的方法,我宁愿问你我是否错了。

最佳答案

来自 Java Docs ,

The for-each loop hides the iterator, so you cannot call remove. Therefore, the for-eachloop is not usable for filtering. Similarly it is not usable for loops where you need to replace elements in a list or array as you traverse it. Finally, it is not usable for loops that must iterate over multiple collections in parallel. These shortcomings were known by the designers, who made a conscious decision to go with a clean, simple construct that wouldcover the great majority of cases.

所以,简单来说,不可能填充数组。

关于java - 对于每个循环 : can we populate an array?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18169703/

相关文章:

java - 了解Java声音API : Finding MIC on Mixer

java - 在 Java 中使用 WSDL 访问 Web 服务的最快方法

java - 当我尝试从 HashSet 中删除不存在的元素时,为什么它没有给出运行时错误?

java - 在 Swing 事件中启动线程

java - 如何删除字符串池中未引用的值?

java - Spring rest Controller "MultipartFile[] multipartFiles"总是接收 [] 或空文件

java - Selenium webdriver 在 webelement 列表中存储 webelement

Java Math.random() 有多随机?

java - 如何实现 TabLayout 的选项卡从左或右开始,但不是从中心开始?

java - 遍历所有 SimpleTextBox