<分区>
我想在 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
}
}
如何在不将它们设置为全局变量的情况下使用它们?