我有以下方法执行树遍历并执行一些操作:
public int method(){
int retVal;
Tree t;
//initialization of t
t.accept(() -> ++retVal)); //error, variable is not
//effectively final
return retVal;
}
在哪里
public interface Visitor{
public void visitNode();
}
public interface Tree{
/**
* Traverses this tree and perform some action in each node
*/
public void accept(Visitor v);
//other methods omitted
}
有没有办法解决这个问题?
最佳答案
没有任何不涉及传入对象而不是局部变量的方法。但是,您可以执行以下任一操作:
- 使用
AtomicInteger
- 使用
int[1]
- (如果适用)将
int
移动为实例字段
关于java - 有没有一种方法/解决方法来增加 java8 中的整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36988336/