foreach无法给外部变量赋值(Local variable decimal defined in an enclosing scope must be final or effectively )
生活随笔
收集整理的這篇文章主要介紹了
foreach无法给外部变量赋值(Local variable decimal defined in an enclosing scope must be final or effectively )
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
報錯信息如下:
??
Local variable decimal defined in an enclosing scope must be final or effectively final
原因:在foreach中沒有權限將外部變量地址改變。
解決方案:使用變量的Holder
解決前:此時會報錯
BigDecimal decimal = BigDecimal.ZERO; list.forEach(e -> {decimal = decimal.add(BigDecimal.ZERO);});解決后:
BigDecimalHolder decimalHolder = new BigDecimalHolder(BigDecimal.ZERO); list.forEach(e -> {decimalHolder.value = decimalHolder.value.add(BigDecimal.ONE); });?
總結
以上是生活随笔為你收集整理的foreach无法给外部变量赋值(Local variable decimal defined in an enclosing scope must be final or effectively )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL锁机制(myisam表所与in
- 下一篇: Eureka控制台下unavailabl