Arraylist 和 Linkedlist 的区别
来源:互联网 发布:电脑锣走刀路软件 编辑:程序博客网 时间:2024/06/05 20:37
首先我们摘录JDK的 java.util包中对于这两个类的部分定义
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable{ private static final long serialVersionUID = 8683452581122892189L; /** * Default initial capacity. */ private static final int DEFAULT_CAPACITY = 10; /** * Shared empty array instance used for empty instances. */ private static final Object[] EMPTY_ELEMENTDATA = {}; /** * Shared empty array instance used for default sized empty instances. We * distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when * first element is added. */ private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; /** * The array buffer into which the elements of the ArrayList are stored. * The capacity of the ArrayList is the length of this array buffer. Any * empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA * will be expanded to DEFAULT_CAPACITY when the first element is added. */ transient Object[] elementData; // non-private to simplify nested class access /** * 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 * @throws IllegalArgumentException if the specified initial capacity * is negative */ public ArrayList(int initialCapacity) { if (initialCapacity > 0) { this.elementData = new Object[initialCapacity]; } else if (initialCapacity == 0) { this.elementData = EMPTY_ELEMENTDATA; } else { throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); } }
public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable{ transient int size = 0; /** * Pointer to first node. * Invariant: (first == null && last == null) || * (first.prev == null && first.item != null) */ transient Node<E> first; /** * Pointer to last node. * Invariant: (first == null && last == null) || * (last.next == null && last.item != null) */ transient Node<E> last; /** * Constructs an empty list. */ public LinkedList() { } }
从此可以看到两者都实现了List接口,而且都不是线程安全的。
两者的区别如下:
1.从底层实现上来说:ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构。
2.对于随机访问和修改操作,ArrayList性能优于LinkedList,因为LinkedList如果要查找某个元素,每次都是从否开始查找;而对于随机插入和删除操作,LinkedList性能优于ArrayList,因为ArrayList的插入和删除操作要移动后面的数据。
因为两个类都不是线程安全的,所以当有多个线程要同时获得类的实例的时候,可以通过Collections的静态方法,在外部给实例对象进行同步:
List list = Collections.synchronizedList(new ArrayList(…));
List list = Collections.synchronizedList(new LinkedList(…));
好了,现在再P一段连个类常用的方法吧,摘自API:
boolean add(E e)Appends the specified element to the end of this list.
void add(int index, E element)Inserts the specified element at the specified position in this list.
boolean contains(Object o)Returns true if this list contains the specified element.
E get(int index)Returns the element at the specified position in this list.
boolean isEmpty()Returns true if this list contains no elements.
E remove(int index)Removes the element at the specified position in this list.
boolean remove(Object o)Removes the first occurrence of the specified element from this list, if it is present.
E set(int index, E element)Replaces the element at the specified position in this list with the specified element.
int size()Returns the number of elements in this list.
1 0
- 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的区别
- 【PAT】PAT那些破事
- CF Educational Codeforces Round 15(A~D)
- Linux(centos6.8) 学习【1】
- iPhone实现XML的POST传递并接收数据
- F28027第六课---内存解读
- Arraylist 和 Linkedlist 的区别
- clear my mac 3 破解版下载
- Visual Studio Code --GO开发工具配置
- Android下使用TcpDump抓包Wireshark分析数据
- Uva 10369 (最小生成树)
- AndroidStudio Mac OS X 10.5+ 快捷键整理
- Android网络编程
- HDU 5702 Solving Order
- Linux环境下动态库和静态库的创建和引用