Arraylist 和 Linkedlist 的区别

首先我们摘录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.

你可能感兴趣的:(线程同步,ArrayList,LinkedList)