一个简单的 for 语句是:
for(int i = 0/*A Optional*/; i < 10/*B Optional*/; i++/*C Optional*/) {
}
现在for语句的初始化代码(A)中可以初始化多少个变量? 另外,我将如何在 for 语句的初始化代码 (A) 中初始化这些变量?
最佳答案
想写多少就写多少,但声明仅限于同一类型。 (对于初始化,请参阅下面的扩展和 Wugs 的回答。)
for (int a = 1, b = 2, c = 3 ; ; ) {
break;
}
(可选)ForInit部分可以是:
If the ForInit code is a list of statement expressions (§14.8), the expressions are evaluated in sequence from left to right; their values, if any, are discarded.
If the ForInit code is a local variable declaration, it is executed as if it were a local variable declaration statement (§14.4) appearing in a block.
If the ForInit part is not present, no action is taken.
上面带有声明的示例代码的格式为 for(LocalVariableDeclaration;;)
。
关于java - 在for语句的初始化中可以初始化多少个变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11657341/