小编Csa*_*rog的帖子

stream.foreach中的java 8局部变量

我想在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)

我怎样才能使用它们将它们设置为全局变量?

java lambda local-variables java-8 java-stream

4
推荐指数
2
解决办法
6916
查看次数

标签 统计

java ×1

java-8 ×1

java-stream ×1

lambda ×1

local-variables ×1