在我的哈希集代碼 中,我想實現ConcurrentModificationException
,以便當任何人試圖在迭代器之後添加或刪除時,它將被拋出。在HashSt代碼中執行throw 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;
}
你看看'AbstractList $ Itr'中的next()方法嗎? – prasanth 2013-04-29 04:38:25