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

我怕爱的太早我们不能终老 提交于 2020-01-11 09:46:10

问题


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 

}

What is that for loop ? some kind of a Matlab syntax ?

Is there any other way to write that for loop ?

Regards


回答1:


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.




回答2:


That is a for-each loop (also called an 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)




回答3:


It is the enhanced for statement. See section 14.14.2. The enhanced for statement of the Java Language Specification.




回答4:


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<Integer> list = new ArrayList<Integer>();

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.




回答5:


it means "for each son in successors, where the type of son is Node"




回答6:


This is one representation of your for loop

for(int i=0;i<successors.size();i++){

        Node myNode = successors.get(i);
    }

This is not a for loop but still you could do this.

Iterator<Node> itr = successors.iterator();

    while(itr.hasNext()){
        Node myNode = itr.next();
        // your logic
    }



回答7:


it is called enhanced for loop, instead of using iterator to iterate over a collection you can simply use this for loop

using iterators

Iterator<String> i=a.iterator();

while(i.hasNext())
{
String s=i.next();
}

you can simply use the enhanced for loop.

for(String s : a)
{
 .. do something
}

so it is just a syntactic sugar introduced in Java 5, that do the same functionality of the iterators and it uses the iterator internally. The class should implement the Iterator interface in order to use this for loop

class Node<T> implements Iterator<String>{

@Override
public boolean hasNext() {
    // TODO Auto-generated method stub
    return false;
}

@Override
public String next() {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void remove() {
    // TODO Auto-generated method stub

}



}


来源:https://stackoverflow.com/questions/13516053/for-loop-in-the-form-of-for-a-b-c-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!