Java for 循环语法:“for (T obj :objects)”

发布于 2024-12-09 22:20:26 字数 135 浏览 2 评论 0原文

我遇到了一些以前从未见过的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

酒儿 2024-12-16 22:20:27

它称为for-each增强for 语句。 请参阅JLS §14.14.2

它是编译器提供的语法糖,用于迭代Iterable 和数组。以下是迭代列表的等效方法:

List<Foo> foos = ...;
for (Foo foo : foos)
{
    foo.bar();
}

// equivalent to:
List<Foo> foos = ...;
for (Iterator<Foo> iter = foos.iterator(); iter.hasNext();)
{
    Foo foo = iter.next();
    foo.bar();
}

这是迭代数组的两种等效方法:

int[] nums = ...;
for (int num : nums)
{
    System.out.println(num);
}

// equivalent to:
int[] nums = ...;
for (int i=0; i<nums.length; i++)
{
    int num = nums[i];
    System.out.println(num);
}

进一步阅读

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 Iterables and arrays. The following are equivalent ways to iterate over a list:

List<Foo> foos = ...;
for (Foo foo : foos)
{
    foo.bar();
}

// equivalent to:
List<Foo> foos = ...;
for (Iterator<Foo> iter = foos.iterator(); iter.hasNext();)
{
    Foo foo = iter.next();
    foo.bar();
}

and these are two equivalent ways to iterate over an array:

int[] nums = ...;
for (int num : nums)
{
    System.out.println(num);
}

// equivalent to:
int[] nums = ...;
for (int i=0; i<nums.length; i++)
{
    int num = nums[i];
    System.out.println(num);
}

Further reading

影子是时光的心 2024-12-16 22:20:27

变量 objectSummary 保存从 objectListing.getObjectSummaries() 返回的 S3ObjectSummary 类型的当前对象,并迭代该集合。

以下是来自 的增强型 for 循环示例 Java 教程

class EnhancedForDemo {
 public static void main(String[] args){
      int[] numbers = {1,2,3,4,5,6,7,8,9,10};
      for (int item : numbers) {
        System.out.println("Count is: " + item);
      }
 }
}

在此示例中,变量 item 保存数字数组中的当前值。

输出如下:

Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10

希望这有帮助!

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

class EnhancedForDemo {
 public static void main(String[] args){
      int[] numbers = {1,2,3,4,5,6,7,8,9,10};
      for (int item : numbers) {
        System.out.println("Count is: " + item);
      }
 }
}

In this example, the variable item holds the current value from the numbers array.

Output is as follows:

Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10

Hope this helps !

抠脚大汉 2024-12-16 22:20:27

是的...这是针对java中的每个循环。

通常,当您从数据库检索数据或对象时,此循环会变得有用。

语法:

for(Object obj : Collection obj)
{
     //Code enter code here
}

示例:

for(User user : userList)
{
     System.out.println("USer NAme :" + user.name);
   // etc etc
}

这适用于每个循环。

它会自动递增。从采集到的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 :

for(Object obj : Collection obj)
{
     //Code enter code here
}

Example :

for(User user : userList)
{
     System.out.println("USer NAme :" + user.name);
   // etc etc
}

This is for each loop.

it will incremental by automatically. one by one from collection to USer object data has been filled. and working.

埋情葬爱 2024-12-16 22:20:27

这就是 foreach 循环语法。它循环遍历 objectListing.getObjectSummaries() 返回的集合中的每个对象。

That's the for each loop syntax. It is looping through each object in the collection returned by objectListing.getObjectSummaries().

北城挽邺 2024-12-16 22:20:27

对于 objectListing.getObjectSummaries() 中的每个 S3ObjecrSummary,

它循环遍历集合中的每个项目

for each S3ObjecrSummary in objectListing.getObjectSummaries()

it's looping through each item in the collection

顾冷 2024-12-16 22:20:27

是的,这就是所谓的 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.
}

予囚 2024-12-16 22:20:27
public class ForEachLoopExample {

    public static void main(String[] args) {

        System.out.println("For Each Loop Example: ");

        int[] intArray = { 1,2,3,4,5 };

        //Here iteration starts from index 0 to last index
        for(int i : intArray)
            System.out.println(i);
    }

}
public class ForEachLoopExample {

    public static void main(String[] args) {

        System.out.println("For Each Loop Example: ");

        int[] intArray = { 1,2,3,4,5 };

        //Here iteration starts from index 0 to last index
        for(int i : intArray)
            System.out.println(i);
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文