我正在扩展ArrayList以创建一个自定义ArrayList,可以在迭代时使用常规的ArrayList方法进行修改。为此,我还创建了一个Iterator。
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
}
}
在我的`hasNext()`和`next()`方法中,我需要确保列表没有被修改(它可以在任何其他时间被修改)。因此,我需要在`synchronized()`块中引用我的封闭类型。