for loop - Java ":" operator in a For expression -
i ran across interesting use of ":" operator while writing unit tests else's code. looks this:
for(class classinstance : instanceofotherclass){ //do irrelevant stuff }
i have never seen ":" operator used without "?", when writing ternary statement. i've done fair amount of googling, , can't seem find sensible answers regarding how should read this...
am over-complicating this? has seen before?
for future reference, had typed:
java 7 loop :
into google, second result have helped you. defense, if don't put "7" there, have been more difficult find solution (not because introduced in java 7, in java 5, because if it's supported, putting 7 gives higher probability of finding up-to-date documentation). example of enhanced loop or for-each loop.
in short:
enhancedforstatement:
for ( formalparameter : expression ) statement
where expression
must iterable
or array type.
in simpler terms (array example, note implements iterable
can used):
string[] words = new string[]{"this","is","the","end"}; (int = 0; < words.length; i++) { system.out.println(words[i]); }
written for-each loop is:
for (string s : words) { system.out.println(s); }
if you're wondering efficiency, check this post out.
Comments
Post a Comment