首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

LinkedList中addAll(int index, Collection< extends E> c)函数分析

2012-12-22 
LinkedList中addAll(int index, Collection? extends E c)函数分析public boolean addAll(int index, Co

LinkedList中addAll(int index, Collection<? extends E> c)函数分析

 public boolean addAll(int index, Collection<? extends E> 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<E> successor = (index==size ? header : entry(index));        Entry<E> predecessor = successor.previous;for (int i=0; i<numNew; i++) {            Entry<E> e = new Entry<E>((E)a[i], successor, predecessor);            predecessor.next = e;            predecessor = e;        }        successor.previous = predecessor;        size += numNew;        return true;    }


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

热点排行