【发布时间】:2017-01-13 08:39:00
【问题描述】:
在我的 Java 8 代码中,
public ChangePersonsName(String email, final String password, final String wantedUsername, final String uuid, final long time, int latency, int[] requests, int[] proxyRequests) throws IOException {
final AtomicReference<Object> token = new AtomicReference<Object>();
final AtomicReference<ArrayList<?>> newHeaders = new AtomicReference<ArrayList<?>>();
new Thread(() -> {
boolean lock = true;
while (lock) {
if (time - System.currentTimeMillis() > 60000) continue;
Map<Header[], String> loginResults = null;
try {
loginResults = this.login(email, password, uuid);
}
catch (IOException e) {
e.printStackTrace();
}
String token = loginResults.entrySet().iterator().next().getValue();
Header[] headers = loginResults.entrySet().iterator().next().getKey();
newHeaders.set(new ArrayList<Object>());
for (Header header : headers) {
if (!header.toString().startsWith("Set-Cookie:")) continue;
((List<BasicHeader>)newHeaders.get()).add(new BasicHeader("Cookie", header.toString().split("Set-Cookie: ")[1]));
}
lock = false;
}
}
).start();
new Timer().schedule(new TimerTask(){
你会注意到
String token = loginResults.entrySet().iterator().next().getValue();
抛出编译错误,
Lambda 表达式的局部变量标记不能重新声明在封闭范围内定义的另一个局部变量。
我的问题是,如何解决这个问题?我对Java很陌生,我可能应该知道如何解决这个问题,但我不知道。
【问题讨论】:
-
final AtomicReference<Object> token = new AtomicReference<Object>();您有两个同名变量 (token)。这在消息中说 local variable token cannot redeclare another local variable defined in an enclosure scope. -
“如何解决这个问题?” 将其命名为其他名称。