Java for 循环语法:“for (T obj :objects)”
我遇到了一些以前从未见过的 Java 语法。我想知道是否有人可以告诉我这里发生了什么事。
for (ObjectType objectName : collectionName.getObjects())
I came across some Java syntax that I haven't seen before. I was wondering if someone could tell me what's going on here.
for (ObjectType objectName : collectionName.getObjects())
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
它称为for-each 或增强
for
语句。 请参阅JLS §14.14.2。它是编译器提供的语法糖,用于迭代
Iterable
和数组。以下是迭代列表的等效方法:这是迭代数组的两种等效方法:
进一步阅读
It's called a for-each or enhanced
for
statement. See the JLS §14.14.2.It's syntactic sugar provided by the compiler for iterating over
Iterable
s and arrays. The following are equivalent ways to iterate over a list:and these are two equivalent ways to iterate over an array:
Further reading
变量 objectSummary 保存从 objectListing.getObjectSummaries() 返回的 S3ObjectSummary 类型的当前对象,并迭代该集合。
以下是来自 的增强型 for 循环示例 Java 教程
在此示例中,变量 item 保存数字数组中的当前值。
输出如下:
希望这有帮助!
The variable objectSummary holds the current object of type S3ObjectSummary returned from the objectListing.getObjectSummaries() and iterate over the collection.
Here is an example of this enhanced for loop from Java Tutorials
In this example, the variable item holds the current value from the numbers array.
Output is as follows:
Hope this helps !
是的...这是针对java中的每个循环。
通常,当您从数据库检索数据或对象时,此循环会变得有用。
语法:
示例:
这适用于每个循环。
它会自动递增。从采集到的US对象数据一一被填满。和工作。
yes... This is for each loop in java.
Generally this loop is become useful when you are retrieving data or object from the database.
Syntex :
Example :
This is for each loop.
it will incremental by automatically. one by one from collection to USer object data has been filled. and working.
这就是 foreach 循环语法。它循环遍历
objectListing.getObjectSummaries()
返回的集合中的每个对象。That's the for each loop syntax. It is looping through each object in the collection returned by
objectListing.getObjectSummaries()
.对于 objectListing.getObjectSummaries() 中的每个 S3ObjecrSummary,
它循环遍历集合中的每个项目
for each S3ObjecrSummary in objectListing.getObjectSummaries()
it's looping through each item in the collection
是的,这就是所谓的 for-each 循环。
collectionName 中的对象将从该集合的开头开始一个接一个地分配给创建的对象引用“objectName”。因此,在循环的每次迭代中,都会为“objectName”分配“collectionName”集合中的一个对象。
当“collectionName”集合的所有项目(对象)都已完成分配或只是要获取的对象结束时,循环将终止。
for (ObjectType objectName : collectionName.getObjects()){
//循环体>
//您可以根据需要在此处使用“objectName”,并且 // 在每次迭代中将由它代表不同的对象。
}
Yes, It is called the for-each loop.
Objects in the collectionName will be assigned one after one from the beginning of that collection, to the created object reference, 'objectName'. So in each iteration of the loop, the 'objectName' will be assigned an object from the 'collectionName' collection.
The loop will terminate once when all the items(objects) of the 'collectionName' Collection have finished been assigning or simply the objects to get are over.
for (ObjectType objectName : collectionName.getObjects()){
//loop body>
//You can use the 'objectName' here as needed and different objects will be //reepresented by it in each iteration.
}