For loop in the form of : “for (A b : c)” in Java

前端 未结 7 970
耶瑟儿~
耶瑟儿~ 2021-01-13 12:32

This is the first time that I\'ve seen this kind of syntax :

// class Node
public class Node { 

...
...

}

public class Otherclass { ... }

Otherclass gra         


        
7条回答
  •  一个人的身影
    2021-01-13 13:30

    It is called Enhanced for-loop. It basically iterates over a Collection by fetching each element in sequence. So you don't need to access elements on index.

    List list = new ArrayList();
    
    for (int val: list) {
        System.out.println(val);  // Prints each value from list
    }
    

    See §14.14.2 - Enhanced for loop section of Java Language Specification.

提交回复
热议问题