zoukankan      html  css  js  c++  java
  • 2 ArrayList 与LinkedList 详解

    List 是有序、可重复的容器。List中每个元素都有索引标记,可以根据元素的索引标记访问元素,从而精确控制这些元素。

    List 接口常用的实现类:ArrayList、LinkedList、Vector。

     

    一、ArrayList

    ArrayList 底层是用数组实现。特点:查询效率高,增删效率低,线程不安全。增删操作较多的场景使用LinkedList,线程安全使用Vector 或者封装的线程安全集合类。

     

    1、ArrayList

    public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable

    ArrayList 通过动态数组实现,且实现了所有可选列表操作,允许包括null在内的所有元素。除了实现List接口外,此类还提供一些方法来操作内部用来存储列表的数组的大小。

    2、源码解析

    ArrayList 由动态对象数组实现,默认构造方法创建一个空数组。第一次添加元素时,容量扩充为10,之后的扩充算法是原容量1.5倍。

    (1) 添加方法

      /**
         * Appends the specified element to the end of this list.
         *
         * @param e element to be appended to this list
         * @return <tt>true</tt> (as specified by {@link Collection#add})
         */
        public boolean add(E e) {
            ensureCapacityInternal(size + 1);  // Increments modCount!!
            elementData[size++] = e;
            return true;
        }

     

    (2) 扩容方法

        private void ensureCapacityInternal(int minCapacity) {
            if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
                minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
            }
    
            ensureExplicitCapacity(minCapacity);
        }
     /**
         * Increases the capacity to ensure that it can hold at least the
         * number of elements specified by the minimum capacity argument.
         *
         * @param minCapacity the desired minimum capacity
         */
        private void grow(int minCapacity) {
            // overflow-conscious code
            int oldCapacity = elementData.length;
            int newCapacity = oldCapacity + (oldCapacity >> 1);
            if (newCapacity - minCapacity < 0)
                newCapacity = minCapacity;
            if (newCapacity - MAX_ARRAY_SIZE > 0)
                newCapacity = hugeCapacity(minCapacity);
            // minCapacity is usually close to size, so this is a win:
            elementData = Arrays.copyOf(elementData, newCapacity);
        }

    (3) 删除方法

        public E remove(int index) {
            rangeCheck(index);
    
            modCount++;
            E oldValue = elementData(index);
    
            int numMoved = size - index - 1;
            if (numMoved > 0)
                System.arraycopy(elementData, index+1, elementData, index,
                                 numMoved);
            elementData[--size] = null; // clear to let GC do its work
    
            return oldValue;
        }
        public boolean remove(Object o) {
            if (o == null) {
                for (int index = 0; index < size; index++)
                    if (elementData[index] == null) {
                        fastRemove(index);
                        return true;
                    }
            } else {
                for (int index = 0; index < size; index++)
                    if (o.equals(elementData[index])) {
                        fastRemove(index);
                        return true;
                    }
            }
            return false;
        }

    3 ArrayList的特点

    3.1 动态数组不适合做删除、插入操作。

    3.2 为了防止数组动态扩充过多,建议创建时给定初始容量。

    3.3 多线程不安全,适合在单线程使用。

    3.4 建议存储同类型的对象。

     

    4 ArrayList 扩展方法

    addAll(Collection c)

    removeAll(Collection c)

    retainAll(Collection c)

    containsALll(Collection c)

    indexOf(Object o)

    lastIndexOf(Object o)

     

    二、LinkedList

     

    1 LinkedList

     

    public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable

     

    实现原理,通过双向链表实现的。适合插入、删除操作。

     

     

    2 节点 

    LinkedList使用双向链表实现,而双向链表通过节点来实现,我们来看看节点的定义。

    复制代码
    private static class Node<E> {
            E item;
            Node<E> next;
            Node<E> prev;
    
            Node(Node<E> prev, E element, Node<E> next) {
                this.item = element;
                this.next = next;
                this.prev = prev;
            }
    }
    复制代码

     

    3 元素操作

    LinkedList默认大小为0,没有扩容机制,每增加一个元素,容量加1。

    (1) add 

        public boolean add(E e) {
            linkLast(e);
            return true;
        }

     

    复制代码
        void linkLast(E e) {
            final Node<E> l = last;
            final Node<E> newNode = new Node<>(l, e, null);
            last = newNode;
            if (l == null)
                first = newNode;
            else
                l.next = newNode;
            size++;
            modCount++;
        }
    复制代码

     

    (2) remove

        public E remove(int index) {
            checkElementIndex(index);
            return unlink(node(index));
        }

     

    复制代码
        E unlink(Node<E> x) {
            // assert x != null;
            final E element = x.item;
            final Node<E> next = x.next;
            final Node<E> prev = x.prev;
    
            if (prev == null) {
                first = next;
            } else {
                prev.next = next;
                x.prev = null;
            }
    
            if (next == null) {
                last = prev;
            } else {
                next.prev = prev;
                x.next = null;
            }
    
            x.item = null;
            size--;
            modCount++;
            return element;
        }
    复制代码

     

    (3) get

        public E get(int index) {
            checkElementIndex(index);
            return node(index).item;
        }

     

    复制代码
        Node<E> node(int index) {
            // assert isElementIndex(index);
    
            if (index < (size >> 1)) {
                Node<E> x = first;
                for (int i = 0; i < index; i++)
                    x = x.next;
                return x;
            } else {
                Node<E> x = last;
                for (int i = size - 1; i > index; i--)
                    x = x.prev;
                return x;
            }
        }
    复制代码

     

    4、其他方法

    getFirst()  

    getLast()

    addFirst()   

    addLast() 

    removeFirst()   

    removeLast()

     

     三、ArrayList与LinkedList 区别

    1、线程安全

    ArrayList 和 LinkedList 都是不同步的,也就是不保证线程安全;

     

    2、底层数据结构

     Arraylist 底层使用的是Object动态数组;LinkedList 底层使用的是双向链表数据结构(JDK1.6之前为循环链表,JDK1.7取消了循环。注意双向链表和双向循环链表的区别,下面有介绍到!)

     

    3、插入和删除是否受元素位置的影响

     (1) ArrayList 采用数组存储,所以插入和删除元素的时间复杂度受元素位置的影响。 比如:执行add(E e)方法的时候, ArrayList 会默认在将指定的元素追加到此列表的末尾,这种情况时间复杂度就是O(1)。但是如果要在指定位置 i 插入和删除元素的话(add(int index, E element))时间复杂度就为 O(n-i)。因为在进行上述操作的时候集合中第 i 和第 i 个元素之后的(n-i)个元素都要执行向后位/向前移一位的操作。

    (2) LinkedList 采用链表存储,所以插入,删除元素时间复杂度不受元素位置的影响,都是近似 O(1)而数组为近似 O(n)。

     

    4、是否支持快速随机访问

    LinkedList 不支持高效的随机元素访问,而 ArrayList 支持。快速随机访问就是通过元素的序号快速获取元素对象(对应于get(int index)方法)。

     

    5、内存空间占用

    ArrayList的空 间浪费主要体现在在list列表的结尾会预留一定的容量空间,而LinkedList的空间花费则体现在它的每一个元素都需要消耗比ArrayList更多的空间(因为要存放直接后继和直接前驱以及数据)。

     

  • 相关阅读:
    全局索引 truncate有数据的分区,索引失效,没数据的分区,索引不失效
    数组的操作
    调存储过程shell
    Flex中的折线图
    SecurityError:Error #2048:安全沙箱冲突
    Flex中对表格中某列的值进行数字格式化并求百分比
    Flex中对表格中某列的值进行数字格式化
    ORA-00904:"T1"."AREA_ID" :标识符无效
    严重:IOException while loading persisted sessions:java.io.EOFException.
    开放的平台、向上的文化——揭秘万达电商(4)
  • 原文地址:https://www.cnblogs.com/Latiny/p/10705934.html
Copyright © 2011-2022 走看看