• ArrayList和LinkedList的区别


    以下为jdk1.6中ArrayList中的构造函数源码。

    1.     public ArrayList(int initialCapacity) {
    2.         super();
    3.         if (initialCapacity < 0)
    4.             throw new IllegalArgumentException("Illegal Capacity: "+
    5.                                                initialCapacity);
    6.         this.elementData = new Object[initialCapacity];
    7.     }
    8.     /**
    9.      * Constructs an empty list with an initial capacity of ten.
    10.      */
    11.     public ArrayList() {
    12.         this(10);
    13.     }

    可以看出在建立ArrayList对象的时候。
    默认建立了一个长度为10的Object数组。但是这只是ArrayList 的实现方式。
    在LinkedList中是这样的。

    1. public LinkedList() {
    2. header.next = header.previous = header;
    3. }

    如果有C 基础的话。应该知道这是链表实现的。
    这就是为什么LinkedList的增删效率高!查询效率低!而ArrayList 的查询效率高!增删效率低!
    这就是说其实数组只是集合实现的一种方式。

  • 相关阅读:
    phpcms页面替换
    phpcms笔记
    php头像上传预览
    phpcms后台管理
    php写流程管理
    php写留言板
    php人员权限管理(RBAC)
    单例模式
    Effective C++笔记——day01
    C++Primer笔记-----day08
  • 原文地址:https://www.cnblogs.com/gameoverit/p/6165408.html
Copyright © 2020-2023  润新知