更新时间:2023-11-29 18:16:16
您不能将int用作变量,因为必须是最终的才能在流中使用。
You can't use an int as variable because it must be final to be used in a stream.
但是你可以创建一个包装int的类。
But You can create a class wrapping the int.
然后声明保持此类为最终的变量。
Then declare the variable holding this class as final.
更改内部int变量的内容。
Changing the content of the inner int variable.
public void findMax(List<List<Route>> routeLists) {
final IntWrapper dWrapper = new IntWrapper();
routeLists.forEach(e-> {
e.forEach(ee -> {
dWrapper.value += ee.getDistance();
});
});
int d = dWrapper.value;
... doing some other operation with d
}
public class IntWrapper {
public int value;
}