LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 230|回复: 0

LinkedList实现 - addAll()

[复制链接]
发表于 2023-12-20 15:05:29 | 显示全部楼层 |阅读模式

addAll()

addAll(index, c) 实现方式并不是直接调用add(index,e)来实现,主要是因为效率的问题,另一个是fail-fast中modCount只会增加1次;

  1.     /**
  2.      * Appends all of the elements in the specified collection to the end of
  3.      * this list, in the order that they are returned by the specified
  4.      * collection's iterator.  The behavior of this operation is undefined if
  5.      * the specified collection is modified while the operation is in
  6.      * progress.  (Note that this will occur if the specified collection is
  7.      * this list, and it's nonempty.)
  8.      *
  9.      * @param c collection containing elements to be added to this list
  10.      * @return {@code true} if this list changed as a result of the call
  11.      * @throws NullPointerException if the specified collection is null
  12.      */
  13.     public boolean addAll(Collection<? extends E> c) {
  14.         return addAll(size, c);
  15.     }

  16.     /**
  17.      * Inserts all of the elements in the specified collection into this
  18.      * list, starting at the specified position.  Shifts the element
  19.      * currently at that position (if any) and any subsequent elements to
  20.      * the right (increases their indices).  The new elements will appear
  21.      * in the list in the order that they are returned by the
  22.      * specified collection's iterator.
  23.      *
  24.      * @param index index at which to insert the first element
  25.      *              from the specified collection
  26.      * @param c collection containing elements to be added to this list
  27.      * @return {@code true} if this list changed as a result of the call
  28.      * @throws IndexOutOfBoundsException {@inheritDoc}
  29.      * @throws NullPointerException if the specified collection is null
  30.      */
  31.     public boolean addAll(int index, Collection<? extends E> c) {
  32.         checkPositionIndex(index);

  33.         Object[] a = c.toArray();
  34.         int numNew = a.length;
  35.         if (numNew == 0)
  36.             return false;

  37.         Node<E> pred, succ;
  38.         if (index == size) {
  39.             succ = null;
  40.             pred = last;
  41.         } else {
  42.             succ = node(index);
  43.             pred = succ.prev;
  44.         }

  45.         for (Object o : a) {
  46.             @SuppressWarnings("unchecked") E e = (E) o;
  47.             Node<E> newNode = new Node<>(pred, e, null);
  48.             if (pred == null)
  49.                 first = newNode;
  50.             else
  51.                 pred.next = newNode;
  52.             pred = newNode;
  53.         }

  54.         if (succ == null) {
  55.             last = pred;
  56.         } else {
  57.             pred.next = succ;
  58.             succ.prev = pred;
  59.         }

  60.         size += numNew;
  61.         modCount++;
  62.         return true;
  63.     }
复制代码


------
原文链接:https://pdai.tech/md/java/collection/java-collection-LinkedList.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表