從原始碼看ArrayList和Vector的真正區別

來源:互聯網
上載者:User

今天正好看到了這個東西,一直都是看別人的資料,決定自己親自看看源代 碼。JDK版本為 6.0_04 的

1 聲明沒有任何區別

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

2 構造器方法,預設都是10個初始化容量。Vector帶每次增加容量的參數

public ArrayList(int initialCapacity);
public Vector(int initialCapacity);
public Vector(int initialCapacity, int capacityIncrement);

3 add等方法,Vector是同步的方法

ArrayList
public boolean add(E e);
Vector
public synchronized boolean add(E e);

4 尺寸和長度,私人變數不同。

ArrayList.size;
Vector.elementCount;

5 其它方法

Vector 比ArrayList方法多,不過一部分是重複的

public synchronized void copyInto(Object[] anArray);
public synchronized void setSize(int newSize);
public synchronized int capacity();
public Enumeration<E> elements();
public synchronized E elementAt(int index);
public synchronized E firstElement();
public synchronized E lastElement();
public synchronized void setElementAt(E obj, int index) ;
public synchronized void removeElementAt(int index);
public synchronized void insertElementAt(E obj, int index);
public synchronized void addElement(E obj);
public synchronized boolean removeElement(Object obj);
public synchronized void removeAllElements();

原文地址:http://www.java2000.net/p9750

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.