对于每个循环:我们可以填充数组吗? [英] For each loop: can we populate an array?

查看:52
本文介绍了对于每个循环:我们可以填充数组吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用以下每种语法从数组中读取数据:

I can read data from an array with for each syntax:

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 值?
我没有发明这种方法,而是想问你我是否错.

But is it possible to populate the array likewise. Say, with i*2 values?
I failed to invent such a method and would rather ask you whether I'm wrong or not.

推荐答案

来自Java文档

for-each循环隐藏了迭代器,因此您不能调用remove.因此,for-each 循环不可用于过滤.相似地它不适用于需要替换元素中的元素的循环遍历列表或数组.最后,它不适用于循环必须并行迭代多个集合.这些缺点是设计师意识到的,他们意识到决定采用干净,简单的结构来覆盖 绝大多数情况.

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.

因此,用简单的话来说,不可能填充数组.

So, in simple words, its not possible to populate arrays.

这篇关于对于每个循环:我们可以填充数组吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆