ArrayList和LinkedList的区别
来源:互联网 发布:ds18b20工作原理c语言 编辑:程序博客网 时间:2024/06/05 06:41
1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构。
2.对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList要移动指针。
3.对于新增和删除操作add和remove,LinedList比较占优势,因为ArrayList要移动数据。
由于sun的开源所以我们可以从代码的角度来看他们两个之间的区别;
先从构造函数说起
ArrayList 的默认构造函数
public ArrayList() { this(10); }
这个10是什么意思呢?再看看带参数的构造函数就明白了
public ArrayList(int initialCapacity) { super(); if (initialCapacity < 0) throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); this.elementData = new Object[initialCapacity]; }
原来是是为ArrayList 分配了10个Object的数组存储空间。
下面看看LinkedList的构造函数
public LinkedList() { header.next = header.previous = header; }
把链表的指针全部归零,从上面的代码可以看出LinkedList是个双向的链表。
下面再来看看两者的get 和add方法有上面区别
首先来看ArrayList add方法
public boolean add(E e) { ensureCapacity(size + 1); // Increments modCount!! elementData[size++] = e; return true; }
这个函数是什么意思呢?看看就知道了
public void ensureCapacity(int minCapacity) { modCount++; int oldCapacity = elementData.length; if (minCapacity > oldCapacity) { Object oldData[] = elementData; int newCapacity = (oldCapacity * 3)/2 + 1; } if (newCapacity < minCapacity){ newCapacity = minCapacity; // minCapacity is usually close to size, so this is a win: elementData = Arrays.copyOf(elementData, newCapacity); } }
原来这个确保ArrayList 存储空间自动增长的,看了代码就知道原来ArrayList 初始化存储空间的大小是10 然后以后以1.5+1的级数增长,在这个过程中先new 原来空间的1.5倍+1的新空间,然后把原来的数据拷贝到新的空间,所以说ArrayList 的添加数据的性能低于LinkedList,原来原因出在此处,那么以后就可以在new ArrayList 的时候,根据实际数据的多少,大概的指定一下ArrayList 的初始化大小,这样避免的多次数据拷贝,提高了系统性能,哈哈又学到了优化的一招。
接下来继续看LinkedList的add方法
public boolean add(E e) { addBefore(e, header); return true; } private Entry<E> addBefore(E e, Entry<E> entry) { Entry<E> newEntry = new Entry<E>(e, entry, entry.previous); newEntry.previous.next = newEntry; newEntry.next.previous = newEntry; size++; modCount++; return newEntry; }
就是双向链表的添加操作,这是数据结构里面的必修炼的功力之一,在添加的时候只要修改一下指针就ok了,没有ArrayList 的增长空间拷贝数据的步骤,所以总体上看来在add的时候,LinkedList比ArrayList 快。
下面来看看ArrayList 的get方法
public E get(int index) { RangeCheck(index); return (E) elementData[index]; }
RangeCheck 检测访问是否越界,然后根据索引下标直接返回值,果然快。
再来看看LinkedList的get方法
public E get(int index) { return entry(index).element; } private Entry<E> entry(int index) { if (index < 0 || index >= size) throw new IndexOutOfBoundsException("Index: "+index+ ", Size: "+size); Entry<E> e = header; if (index < (size >> 1)) { for (int i = 0; i <= index; i++) e = e.next; } else { for (int i = size; i > index; i--) e = e.previous; } return e; }
一个一个去找是比较的慢,不过还是优化了,如果要找的数小于等于size的一半就从头往后找,要是大于size的一半就从后往前找,LinkedList的get和ArrayList 比起来确实慢了很多。
- linkedlist和arraylist的区别
- ArrayList和LinkedList的区别
- ArrayList和LinkedList的区别
- ArrayList和LinkedList的区别
- ArrayList和LinkedList的区别
- ArrayList和LinkedList的区别
- arraylist和linkedlist的区别!
- LinkedList和ArrayList的区别
- LinkedList和ArrayList的区别
- LinkedList和ArrayList的区别
- ArrayList和LinkedList的区别
- ArrayList和LinkedList的区别
- LinkedList和ArrayList的区别
- ArrayList和LinkedList的区别
- ArrayList和LinkedList的区别
- ArrayList 和linkedlist的区别
- ArrayList和LinkedList的区别
- ArrayList和LinkedList的区别
- 点滴:Java JVM垃圾回收机制
- [Mugeda HTML5技术教程之18]如何在Android应用中使用Mugeda动画内容
- chrome 主页被篡改为hao123?技术宅带你层层深入破之
- PHP 使用header函数设置HTTP头的示例方法 表头 (xlsx下载)
- Linux多线程同步互斥量Mutex详解
- ArrayList和LinkedList的区别
- ffmpeg h264+ts +udp传输
- B - 最少拦截系统
- 107条Javascript的常用语句
- Linux 首次引入 nftables
- 团队协同工具
- 点滴:Java HashMap实现详解
- KeyPress 和KeyDown 、KeyPress之间的区别
- 如何在Windows+VS2005使用最新静态libcurl 7.35.0获取网页数据,支持HTTPS