开发者

I don't understand a condition in a loop

I am reading some code in Java and I don't understand the condition of th开发者_Go百科is loop:

for (Integer label : labelConj) 
{...........
}

"label" is an integer and "labelConj", a set of integers. What does the condition control? I can't find any information in Java tutorials. Thanks in advance.


It's not a condition, it's a foreach loop. It's saying "for each Integer (which will be called label inside the loop body) in the collection of Integers called labelConj, loop." The loop will execute once for each item, and then stop.

This syntax can be used with most of the collection classes in the Java framework, and classes you write can use it if you either inherit from one of those classes, or implement the Iterable interface.


it is a foreach loop. It iterates over all elements (Integers in your example) in labelConj.


This is the same as:

for (int i=0; i<labelConj.length; i++) {
    Integer label = labelConj[i];
    ...
}


This iterate through the List of integers. This is foreach loop in PHP


this is a fast enumeration equivalent to for (Integer label in labelConj)


It's a shorthand way of iterating over a collection of things. Here's a link to some information. Just Google 'enhanced for loop'

http://blogs.oracle.com/CoreJavaTechTips/entry/using_enhanced_for_loops_with


This is a compact form of the for loop (called Enhanced For statement) which loops for all the elements in an array and assigns each element to a given variable (in this case, "label"). See here for ref: http://download.oracle.com/javase/tutorial/java/nutsandbolts/for.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜