我对循环有疑问。hasNext() 似乎总是错误的,因为它只是跳过了循环。但这是不可能的,因为我传递给方法的参数大小是2。为什么迭代器在参数上调用时不起作用?
PS我正在复制并重新分配数组,因为 CopyToWrite 的实现需要它
@Override
public boolean removeAll(Collection<?> collection) {
if (collection == null) {
throw new IllegalArgumentException("The argument cannot be null");
}
int newSize = currentSize;
Product[] newElements = new Product[products.length];
Product[] resultList = new Product[newSize - collection.size()];
System.arraycopy(products, 0, newElements, 0, currentSize);
boolean result = false;
Iterator<?> iterator = collection.iterator();
int index = 0;
while (iterator.hasNext()) {
if (!newElements[index].equals(iterator.next())) {
resultList[index] = newElements[index];
index++;
newSize--;
result = true;
}
}
products = (E[]) resultList;
currentSize = newSize;
return result;
}