在我的哈希集代码中,我想实现一个ConcurrentModificationException
,这样当任何人试图在迭代器之后添加或删除时,它都会抛出。
以下是部分代码:
/** Need to add ConcurrentModificationException stuff*/
public boolean hasNext()
{
if (current != null && current.next != null)
{
return true;
}
for (int b = bucketIndex + 1; b < buckets.length; b++)
{
if (buckets[b] != null)
{
return true;
}
}
return false;
}
/** Need to add ConcurrentModificationException stuff*/
public Object next()
{
if (current != null && current.next != null)
{
current = current.next; // Move to next element in bucket
} else
// Move to next bucket
{
do
{
bucketIndex++;
if (bucketIndex == buckets.length)
{
throw new NoSuchElementException();
}
current = buckets[bucketIndex];
} while (current == null);
}
return current.data;
}