java - 在for语句的初始化中可以初始化多少个变量?

标签 java for-loop initialization

一个简单的 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部分可以是:

  1. 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.

  2. 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.

  3. If the ForInit part is not present, no action is taken.

上面带有声明的示例代码的格式为 for(LocalVariableDeclaration;;)

关于java - 在for语句的初始化中可以初始化多少个变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11657341/

相关文章:

java - 由 : org. hibernate.boot.registry.classloading.spi.ClassLoadingException 引起:无法加载类 [Employee]

java - CWSAD0016W - IBM WAS 节点需要 Web 服务功能包

VB6.0 : initialize method of a User Control called when loading a VB project

c++ - C++ Stringstream初始化

Java 文件列表未使用比较器排序

Matlab - for loop over cell array "foreach"-like 语法循环仅在第一个元素上

python - 如何在 python 中编写 for 循环以仅从 csv 文件中提取唯一值以加载到我的 API 脚本中?

python - 有没有办法改变for循环的迭代器?

java - 如何在 Java 的时间戳实例中设置全零?

java - 将 CXF 服务的客户端 receiveTimeout 设置为 4 分钟以上