相关疑难解决方法(0)

如何从Inner类引用Enclosing类?

我正在扩展ArrayList以创建一个自定义ArrayList,可以在迭代它时使用常规ArrayList方法进行修改.为此,我也创建了一个迭代器.

public class SynchronizedList<E> extends ArrayList<E>
{
    // Fields here

    //Constructors and methods here

public class SynchronizedListIterator<E> implements Iterator<E>
{
    public int index;
    private E current;

    public boolean hasNext()
    {
        synchronized (/* reference to enclosing List object */) {
                    //code goes here
        }
        return false;
    }

    //more methods here
}
}
Run Code Online (Sandbox Code Playgroud)

在我的hasNext()和next()方法中,我需要确保列表未被修改(可以在任何其他时间修改它).因此,我需要在synchronized()块中引用我的封闭类型.

java nested class arraylist synchronized

3
推荐指数
1
解决办法
190
查看次数

标签 统计

arraylist ×1

class ×1

java ×1

nested ×1

synchronized ×1