ArrayList 的相关知识

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

由上面源码可知,Arraylist继承自AbstractList 实现了List ,Cloneable,Serializable,RandomAccess接口.其中Cloneable是克隆接口,Serializable是实现序列化操作的接口,便于对象的传输。而今天重点要描述一下这个RandomAccess。

RandomAccess接口是一个标记接口,实现了它的list集合支持随机访问,目的是是算法在随机和随机访问时显得更灵活。在collections的 binarySearch源码如下

public static <T>
int binarySearch(List<? extends Comparable<? super T>> list, T key) {
if (list instanceof RandomAccess || list.size()<BINARYSEARCH_THRESHOLD)
return Collections.indexedBinarySearch(list, key);
else
return Collections.iteratorBinarySearch(list, key);
}

我们可以看到,他首先会判断这个list是否实现了RandomAccess接口,如果这个条件和list.size()<BINARYSEARCH_THRESHOLD(其中: BINARYSEARCH_THRESHOLD Collections的一个常量(5000),它是二分查找的阀值。)满足其中任何一个则使用Collections的indexedBinarySearch(list,key)方法。否则会调用Collections.iteratorBinarySearch(list,key)方法。

这两者的代码实现分别如下:

indexedBinarySearch 方法:

  1. private static <T>
  2. int indexedBinarySearch(List<? extends Comparable<? super T>> list, T key) {
  3. int low = 0;
  4. int high = list.size()-1;
  5. while (low <= high) {
  6. int mid = (low + high) >>> 1;
  7. Comparable<? super T> midVal = list.get(mid);
  8. int cmp = midVal.compareTo(key);
  9. if (cmp < 0)
  10. low = mid + 1;
  11. else if (cmp > 0)
  12. high = mid - 1;
  13. else
  14. return mid; // key found
  15. }
  16. return -(low + 1);  // key not found
  17. }

indexedBinarySearch 方法是直接通过get来访问元素

iteratorBinarySearch方法

  1. private static <T>
  2. int iteratorBinarySearch(List<? extends Comparable<? super T>> list, T key)
  3. {
  4. int low = 0;
  5. int high = list.size()-1;
  6. ListIterator<? extends Comparable<? super T>> i = list.listIterator();
  7. while (low <= high) {
  8. int mid = (low + high) >>> 1;
  9. Comparable<? super T> midVal = get(i, mid);
  10. int cmp = midVal.compareTo(key);
  11. if (cmp < 0)
  12. low = mid + 1;
  13. else if (cmp > 0)
  14. high = mid - 1;
  15. else
  16. return mid; // key found
  17. }
  18. return -(low + 1);  // key not found
  19. }

iteratorBinarySearch中ListIterator来查找相应的元素

在javadoc中指出人们认识到随机存取和顺序存取之间的区别通常是模糊的。例如,一些列表实现提供了渐进的线性访问时间,如果它们在实践中获得了巨大的、但持续的访问时间。这种接口通常应该实现这个接口。根据经验,列表实现应该实现这个接口,如果对于类的典型实例,这个循环:

for (int i=0, n=list.size(); i < n; i++)
list.get(i);

runs faster than this loop:

     for (Iterator i=list.iterator(); i.hasNext(); )
i.next();
也就是说实现RandomAccess接口的的List可以通过简单的for循环来访问数据比使用iterator访问来的高效快速。
回过头来,我们再说说ArrayList,

{
private static final long serialVersionUID = 8683452581122892189L;

/**
* The array buffer into which the elements of the ArrayList are stored.
* The capacity of the ArrayList is the length of this array buffer.
*/
private transient Object[] elementData;   //ArrayList的底层是一个基数组。

/**
* The size of the ArrayList (the number of elements it contains).
*
* @serial
*/
private int size;

/**
* Constructs an empty list with the specified initial capacity.
*
* @param initialCapacity the initial capacity of the list
* @exception IllegalArgumentException if the specified initial capacity
* is negative
*/
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}

/**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
this(10);    //默认的初始大小为10.

}

public void ensureCapacity(int minCapacity) {  //给定所需的最小容量
modCount++;
int oldCapacity = elementData.length;    //原来数组中的元素大小
if (minCapacity > oldCapacity) {//如果所需的最小容量大于 原数组中数据的个数时。
Object oldData[] = elementData;
int newCapacity = (oldCapacity * 3)/2 + 1;  //则要对旧的数组进行扩容,新数组的容量为原来的1.5倍+1
if (newCapacity < minCapacity)//如果新的容量还是小于最小需求的容量,则将最小需求容量赋给新容量。
newCapacity = minCapacity;
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);//在将就数组中的元素拷贝到新的容量数组中
}
}

LinkedList 的相关知识

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

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

它的增添方法常用的是add(),删除常用的方法是remove(),它的底层是一个双向链表。

对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList要移动指针。

对于增添与删除add和remove,ArrayList需要挪动数据,除非是在数组末尾,需要消耗一定的时间,而LinkedList便可以很轻易地做到。

总结ArrayList 和LinkedList的区别:

1.两者继承的基类不一样。ArrayList 继承自AbstractList,而LinkedList继承自 AbstractSequentialList。

2.底层结构不一样。ArrayList 采用的是动态数组,而LinkedList采用的是双向链表。

3.ArrayList实现了RandomAccess接口,所以在随机访问时效率比较高。而LinkedList需要不停地移动指针。

4.对于增添与删除add和remove,ArrayList需要挪动数据,除非是在数组末尾,需要消耗一定的时间,而LinkedList便可以很轻易地做到。

5。ArrayList 默认初始容量是10,扩容时扩容它旧容量的1.5倍加1.

ArrayList 和 LinkList 的区别的更多相关文章

  1. Java:List,ArrayList和LinkList的区别

    1.大学数据结构中ArrayList是实现了基于动态数组的数据结构,LinkList基于链表的数据结构 2.对于随机访问get和set,ArrayList优于LinkList,因为LinkedList ...

  2. ArrayList和LinkList的区别

    底层实现区别 ArrayList 底层实现就是数组,且ArrayList实现了RandomAccess,表示它能快速随机访问存储的元素,通过下标 index 访问,只是我们需要用 get() 方法的形 ...

  3. List和ArrayList,LinkList的区别

    接口 List<E> 是一个接口: ArrayList<E> 是一个类:是一个实现了List接口的类,因此可以List里面定义的所有的方法都实现了. 1.ArrayList是实 ...

  4. Vector/Arraylist与Linklist的区别

        SDK提供了有序集合接口java.util.List的几种实现,其中三种最为人们熟知的是Vector.ArrayList和    LinkedList.有关这些List类的性能差别是一个经常被 ...

  5. ArrayList和LinkList区别

    ArrayList和LinkList区别 前者是数组的数据结构,后者是链表的数据结构 前者应用于排序和查找,后者应用于插入删除

  6. [置顶] Array ArrayList LinkList的区别剖析

    这是一个面试中我们经常被问到的问题 Array.ArrayList.LinkList之间的区别:Array.ArrayList.LinkList均属于泛型的范畴,都用来存放元素,主要区别是Array是 ...

  7. Android——ArrayList 、LinkList、List 区别 & 迭代器iterator的使用 & HashMap、Hashtable、LinkedHashMap、TreeMap

     ArrayList .LinkList.List 区别 & 迭代器iterator的使用 & HashMap.Hashtable.LinkedHashMap.TreeMap 一.几个 ...

  8. Java集合(2)一 ArrayList 与 LinkList

    目录 Java集合(1)一 集合框架 Java集合(2)一 ArrayList 与 LinkList Java集合(3)一 红黑树.TreeMap与TreeSet(上) Java集合(4)一 红黑树. ...

  9. 面试题——ArrayList和LinkedList的区别

    List概括 先回顾一下List在Collection的框架图: 从图中可以看出: List是一个接口,他继承Collection接口,代表有序的队列. AbstractList是一个抽象类, ,它继 ...

随机推荐

  1. Codeforces 1009G Allowed Letters 最大流转最小割 sosdp

    Allowed Letters 最直观的想法是贪心取, 然后网络流取check可不可行, 然后T了. 想到最大流可以等于最小割, 那么我们状压枚举字符代表的6个点连向汇点是否断掉, 然后再枚举64个本 ...

  2. VM下新建虚拟机并装linux系统

    一.新建虚拟机 1.选择典型----> 2.选择稍后安装操作系统---> 3.选择操作系统和版本----> 4.选择虚拟机存放位置---> 5.配置虚拟机---> 二.l ...

  3. SparkCore| 算子

    RDD RDD(Resilient Distributed Dataset)叫做弹性分布式数据集,是Spark中最基本的数据抽象.代码中是一个抽象类,它代表一个弹性的.不可变.可分区.里面的元素可并行 ...

  4. Git permission denied(public key) 解决方法

    1. 在Linux上: # ssh-keygen       ##一定用 id_rsa.pub # cat /root/.ssh/id_rsa.pub 2. copy 整个文件内容到剪切板 3. 打开 ...

  5. Promise(interesting)

    // 最主要的是理解setTimeout和(浏览器执行程序 || resolve执行位置)的顺序就能吃透了 //(MD 楞是看了我2个小时时间 真的是费脑洞︿( ̄︶ ̄)︿)X.then.then 以下 ...

  6. 2017-11-8—自动控制原理在软硬件方面上的应用和体现

    这个话题讲起来是非常大的,先贴一下百度百科的定义: 自动控制(automatic control)是指在没有人直接参与的情况下,利用外加的设备或装置,使机器.设备或生产过程的某个工作状态或参数自动地按 ...

  7. Wireshark简单使用教程1——附视频

    目录 Wireshark的简介 Wireshark面向的用户 Wireshark的下载安装 Wireshark抓取一个流量包 内容 1.Wireshark的简介 Wireshark(前称Etherea ...

  8. linux 文件 IO 目录操作及文件属性

    ///练习:实现列出某个目录中所有文件属性(文件大小,文件最后修改时间,文件名)//目录名由参数传入 ./dir /home/linux#include <sys/stat.h>#incl ...

  9. linux 标注主机别名

    vi /etc/bashrc # Turn on checkwinsize  shopt -s checkwinsize  [ "$PS1" = "\\s-\\v\\\$ ...

  10. Component template should contain exactly one root element. If you are using v-if on multiple elements, use v-else-if to chain them instead.

    运行代码时,一直报错: 经过查询后才知道,vue模板只能有一个跟对象 我是这样写的 最后修改为 就可以正常运行了