是否可以在有条件的情况下在 Java 中声明变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38766891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to declare a variable within a Java while conditional?
提问by Markus Mitterauer
In Java it is possible to declare a variable in the initialization part of a for-loop:
在 Java 中,可以在for-loop的初始化部分声明一个变量:
for ( int i=0; i < 10; i++) {
// do something (with i)
}
But with the whilestatement this seems not to be possible.
但是根据while声明,这似乎是不可能的。
Quite often I see code like this, when the conditional for the while loop needs to be updated after every iteration:
我经常看到这样的代码,当每次迭代后都需要更新 while 循环的条件时:
List<Object> processables = retrieveProcessableItems(..); // initial fetch
while (processables.size() > 0) {
// process items
processables = retrieveProcessableItems(..); // update
}
Here on stackoverflow I found at least a solutionto prevent the duplicate code of fetching the processable items:
在stackoverflow上,我至少找到了一个解决方案来防止获取可处理项目的重复代码:
List<Object> processables;
while ((processables = retrieveProcessableItems(..)).size() > 0) {
// process items
}
But the variable still has to be declared outside the while-loop.
但是变量仍然必须在while循环之外声明。
So, as I want to keep my variable scopes clean, is it possible to declare a variable within the while conditional, or is there some other solution for such cases?
所以,因为我想保持我的变量范围干净,是否可以在 while 条件中声明一个变量,或者对于这种情况是否有其他解决方案?
回答by Andy Turner
You can write a whileloop using a forloop:
您可以while使用for循环编写循环:
while (condition) { ... }
is the same as
是相同的
for (; condition; ) { ... }
since all three bits in the brackets of the basic for statement declarationare optional:
因为基本 for 语句声明的括号中的所有三位都是可选的:
BasicForStatement:
for ( [ForInit] ; [Expression] ; [ForUpdate] ) Statement
Similarly, you can just rewrite your whileloop as a forloop:
同样,您可以将while循环重写为for循环:
for (List<Object> processables;
(processables = retrieveProcessableItems(..)).size() > 0;) {
// ... Process items.
}
Note that some static analysis tools (e.g. eclipse 4.5) might demand that an initial value is assigned to processables, e.g. List<Object> processables = null. This is incorrect, according to JLS; my version of javacdoes not complain if the variable is left initially unassigned.
请注意,某些静态分析工具(例如 eclipse 4.5)可能要求将初始值分配给processables,例如List<Object> processables = null。根据 JLS 的说法,这是不正确的;javac如果变量最初未分配,我的版本不会抱怨。
回答by Bathsheba
No it's not possible.
不,这是不可能的。
It doesn't really make too much sense either: unlike a forloop where you can set up the initial state of the "looping variable", in a whileloop you test the value of an existingvariable, akin to the conditional checkof the forloop.
它并没有真正太大的意义之一:不像一个for循环,你可以设置“循环变量”的初始状态,在一个while循环中,您测试的价值存在变数,类似的条件检查的的for循环。
Of course, if you're concerned about variables "leaking" into other parts of your code, you could enclose the whole thing in an extra scope block:
当然,如果您担心变量“泄漏”到代码的其他部分,您可以将整个内容包含在一个额外的作用域块中:
{
/*declare variable here*/
while(...){...}
}
Alternatively, convert the whileloop into a forloop.
或者,将while循环转换为for循环。

