gpt4 book ai didi

java - 形式为: "for (A b : c)" in Java的for循环

转载 作者:搜寻专家 更新时间:2023-11-01 03:58:46 31 4
gpt4 key购买 nike

这是我第一次看到这种语法:

// class Node
public class Node {

...
...

}

public class Otherclass { ... }

Otherclass graph = new Otherclass();

// getSuccessors is a method of Otherclass class

Node currentNode ;

List<Node> successors = graph.getSuccessors(currentNode);

// weird for loop

for (Node son : successors) {

// do something

}

for 循环是什么?某种 Matlab 语法?

还有其他方法可以写这个 for 循环吗?

问候

最佳答案

那是一个 for-each 循环(也称为 enhanced-for。)

for (type var : arr) { //could be used to iterate over array/Collections class
body-of-loop
}

The basic for loop was extended in Java 5 to make iteration over arrays and other collections more convenient. This newer for statement is called the enhanced for or for-each

(Documentation)

关于java - 形式为: "for (A b : c)" in Java的for循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13516053/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com