我想在lambda函数中使用局部变量但我得到错误:请参阅代码中的1.和2.点.
class Foo {
int d = 0; // 1. It compiles, but ugly, 2. doesnt compile
public void findMax(List<List<Route>> routeLists) {
int d = 0; // 2.Error : Local variable dd defined in an enclosing scope must be final or effectively final
routeLists.forEach(e-> {
e.forEach(ee -> {
d+=ee.getDistance();
});
});
... doing some other operation with d
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能使用它们将它们设置为全局变量?