[Java] 纯文本查看 复制代码
//创建集合类 代码段1
Collection<String> aCollection = new ArrayList<>();
//添加数据
aCollection.add("Python");
aCollection.add("谢飞");
aCollection.add("斗破苍穹");
aCollection.add("斗破苍穹3");
//创建迭代器对象.iterator()方法
Iterator<String> it = aCollection.iterator();
System.out.println("原先的集合: ");
while (it.hasNext())
{
String nextStr = it.next();
System.out.println(nextStr);
if (nextStr.equals("斗破苍穹"))
{
it.remove(); //位置1
//((ArrayList<String>) aCollection).set(0,"666"); //位置2
//((ArrayList<String>) aCollection).remove(0); //位置3
}
}
System.out.println("----------------------");
System.out.println("删除后: " + aCollection); //
-------------------------------------------------------------------------------[Java] 纯文本查看 复制代码
private class Itr implements Iterator<E> {
int cursor; // index of next element to return 代码段2源码
int lastRet = -1; // index of last element returned; -1 if no such
int expectedModCount = modCount;
// prevent creating a synthetic constructor
Itr() {}
public boolean hasNext() {
return cursor != size;
}
@SuppressWarnings("unchecked")
public E next() {
checkForComodification();
int i = cursor;
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[lastRet = i];
}
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
ArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}......................................
}