在关于数组的for循环中,':'是什么意思? [英] What is meant by ':' in for loops with respect to arrays?

查看:273
本文介绍了在关于数组的for循环中,':'是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码,我无法理解(int element:array)的行是什么意思?

I have got a piece of code and am not able to understand what does the line for (int element : array) mean?

int[] array = new int[] {11, 22, 33, 44, 55};
double average = 0;
for (int element : array){
    average += element;
}





我的尝试:



这是否意味着我们必须执行循环的内容直到最大值。没有。 of index ??



What I have tried:

Does this mean that we have to execute the contents of the loop till the max. no. of indices??

推荐答案

for (int element : array){



表示:对于每个 int 的值 array 元素的值设置为数组中的下一项并执行以下代码块。对每个值重复。


means: for each int value in array set the value of element to the next item in array and execute the following code block. Repeat for each value.


在Java中,这是一个foreach循环,这意味着语言本身负责for循环的初始化器和计数器,你可以像你一样执行操作将。



以下代码,

In Java, that is a foreach loop, which means that the language itself takes care of "initializers and counters" of the for loop and you can perform actions as you would.

This following code,
int[] array = new int[] {11, 22, 33, 44, 55};
double average = 0;
for (int element : array){
    average += element;
}



是简写版的,


Is a short hand version of,

int[] array = new int[] {11, 22, 33, 44, 55};
double average = 0;
for (int el = 0; el < array.length; el++){
    average += array[el];
}



更多信息, For-Each循环 [ ^ ]详细解释了所有内容,并向您展示了如何在Java中使用数组。



另一个主要区别就是在for-each循环中,type指定用于将集合中的每个对象强制转换为的类型。但是,在for-loop-we-all-know中,用作变量,检查条件,增加或减少或执行其他操作。它与数组集合中的对象没有任何关联,而foreach必须提供将对象强制转换为的类型。如果对象无法映射到该类型,则会出现错误 - 哪个?我还不知道。 : - )


For more, The For-Each Loop[^] explains everything in a bit more, and shows you how to use arrays in Java.

Another major difference is that, in the for-each loop, the type specifies the type to be used to cast each object in collection to. However, in the for-loop-we-all-know, that is used as a variable, to check the condition and to increment or decrement or to perform other actions. It has no connection to the objects in the array collection, whereas the foreach must be provided with the type to cast the objects to. If the objects cannot be mapped to that type, there will be error — which? I don't yet know. :-)


这在 java 中称为 foreach 循环..



foreach循环:



This is called foreach loop in java..

foreach loop:

Quote:

Java5中引入的for-each循环。它主要用于遍历数组或集合元素。 for-each循环的优点是它消除了bug的可能性并使代码更具可读性。

The for-each loop introduced in Java5. It is mainly used to traverse array or collection elements. The advantage of for-each loop is that it eliminates the possibility of bugs and makes the code more readable.





优点for-each循环:



1.它使代码更具可读性。

2.它消除了编程错误的可能性。



for-each循环的语法:





Advantage of for-each loop:

1. It makes the code more readable.
2. It eliminates the possibility of programming errors.

Syntax of for-each loop:

for(data_type variable : array | collection){}





回答您的具体疑问:





Answer to your specific doubt:

for (int element : array){
    average += element;
}





此代码段仅表示数组中每个元素的执行以下步骤



快乐学习!



This snippet simply means for each element in the array do the below mentioned steps

Happy learning!


这篇关于在关于数组的for循环中,':'是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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