use*_*283 10 java recursion foreach for-loop
for-each循环在调用方法时是如何工作的,无论是递归还是不同的方法?
例:
for(String permutation : permute(remaining))
{
// Concatenate the first character with the permutations of the remaining chars
set.add(chars.charAt(i) + permutation);
}
Run Code Online (Sandbox Code Playgroud)
顺便说一下,方法permute接受一个String并返回一个set.
谢谢.
根据该语句的Java语言规范enhanced for,表达式:
for ( FormalParameter : Expression ) Statement
Run Code Online (Sandbox Code Playgroud)
执行如下:
for (I #i = Expression.iterator(); #i.hasNext(); ) {
VariableModifiersopt TargetType Identifier =
(TargetType) #i.next();
Statement
}
Run Code Online (Sandbox Code Playgroud)
因此,Expression(必须是类型Iterable)只有iterator()一次调用它的方法.
| 归档时间: |
|
| 查看次数: |
2596 次 |
| 最近记录: |