English 中文(简体)
2. 形成时段的变量——而不是声明
原标题:Initialising a variable in the while expression - not the statement

In the expression of a while loop, is it possible to initialise a variable, then use that as part of the expression?

这可能比较简单:

while (int a = someMethod(), a<b)

可能只是增加另一种方法,因此必须:

private boolean whileLoopTest() {
    int a = someMethod();
    return a<b;
}

public void originalMethod() {
    while (whileLoopTest()) {
        //...

但这似乎只是一线。

EDIT I also don t want to directly compare the method to my variable, as it is compared to several variable, and so if would be a long, unreadable mess. A better example of what I want would be:

while (int a = SomeClass.someStaticMethod(), -1<a && a<b)

It s not true in my case, but this would be a equally valid question if someStaticMethod() took a long time to return - I would only want to call it once. I m fairly new to StackOverflow, so I m not sure if giving other situations where this would apply is what I should be doing.

最佳回答
int a;
while((a = someMethod()) < b){
    //do something
}

共同使用:

BufferedReader fileIn = ...
String line;
while((line = fileIn.readLine()) != null){
    //do something
}
fileIn.close();

/edit
You can do this for your new scenario:

int a;
while(-1 < (a = SomeClass.staticMethod()) && a < b) {
    //do something
}

<代码>&&说明的左手部分一经执行,<代码>的回报值。 一些Class.staticMethod() 储存在 上,该编码载有发言右侧部分。

问题回答

Why not just not assign the value to "a" if you are not using it anyways? while (someMethod() < b) { doSomething() } If you actually do need "a" then your alternate solution would not work. The solution then would be either to save it (which I do not consider unneat) or what Kerrek said.

You can use the function directly without using a local variable like this:

while ( someMethod() < b) { /* ... */}

This, if your method returns intended value. (If you are casting it to a local variable, it s supposed to)

EDIT: For your second question. Your concern is understandable, but if you are assigning that methods value to a local variable inside while loop s boolean expression, in every loop where "While" checks the expression, you are assigning methods return value to local variable, which means you are calling that method in every iteration. That doesn t change anything from my first answer.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签