LinkedList中addAll(int index, Collection


public boolean addAll(int index, Collection c) {
if (index < 0 || index > size)
throw new IndexOutOfBoundsException("Index: "+index+
", Size: "+size);
Object[] a = c.toArray();
int numNew = a.length;
if (numNew==0)
return false;
modCount++;

Entry successor = (index==size ? header : entry(index));
Entry predecessor = successor.previous;
for (int i=0; i Entry e = new Entry((E)a[i], successor, predecessor);
predecessor.next = e;
predecessor = e;
}
successor.previous = predecessor;

size += numNew;
return true;
}


这个函数也很无敌,关键是collection插入的时候保持了有序,主要是那个for循环体里面,后继的元素都在之前被插入的元素之后插入,所以能保持有序。

你可能感兴趣的:(java)