I'm pretty sure the following is legal in Java
for (int var1 = 2, var2 = 3; var1 < 10; ++var1) {
System.out.println(var1);
}
But when I try to run it in the Groovy console, I get this error
unexpected token: =
Are multiple variable declarations unsupported by Groovy or is there another reason why this isn't allowed?
It's a common gotcha for Java Developers. See this link for more detail:
Common gotchas you can use only one count variable .
Excerpts from the link:
for Loops
Another small difference is that you can't initialize more than one variable in the first part of a for loop, so this is invalid:
for (int count = someCalculation(), i = 0; i < count; i++) { ... }
and you'll need to initialize the count variable outside the loop ( a rare case where Groovy is more verbose than Java! ):
int count = someCalculation() for (int i = 0; i < count; i++) { ... }
or you could just skip the whole for loop and use times:
someCalculation().times { ... }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.