对于以下形式的循环:“for(A b:c)”在Java中 [英] For loop in the form of : "for (A b : c)" in Java
本文介绍了对于以下形式的循环:“for(A b:c)”在Java中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我第一次看到这种语法:
This is the first time that I've seen this kind of syntax :
// 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
}
循环是什么?某种Matlab语法?
What is that for loop ? some kind of a Matlab syntax ?
还有其他方法可以为循环编写吗?
Is there any other way to write that for loop ?
问候
推荐答案
每个循环都是一个。您也可以这样写:
It's a for each loop. You could also write it like this:
for(int i = 0; i < successors.size(); i++) {
Node son = successors.get(i);
}
虽然我个人这样做的唯一时间是需要索引时除了访问元素之外的其他事情。
Though the only time I'd personally do that is when the index is needed for doing something other than accessing the element.
这篇关于对于以下形式的循环:“for(A b:c)”在Java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文