Java基礎隨筆1

最近準備跳槽了,可能要從安卓轉作javaweb,抓緊時間複習一下java的基礎內容。正好在github上發現了crossoverJie的Java-Interview項目,就來逐項學習一下。java

————————————————————————————————————————————————————————————————————————————git

ArrayList/Vector 的底層分析

ArrayList

ArrayList 實現於 ListRandomAccess 接口。能夠插入空數據,也支持隨機訪問。github

ArrayList至關於動態數據,其中最重要的兩個屬性分別是: elementData 數組,以及 size 大小。 在調用 add() 方法的時候:web

    public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; }
  • 首先進行擴容校驗。
  • 將插入的值放到尾部,並將 size + 1 。

若是是調用 add(index,e) 在指定位置添加的話:算法

    public void add(int index, E element) { rangeCheckForAdd(index); ensureCapacityInternal(size + 1); // Increments modCount!! //複製,向後移動 System.arraycopy(elementData, index, elementData, index + 1, size - index); elementData[index] = element; size++; }
  • 也是首先擴容校驗。
  • 接着對數據進行復制,目的是把 index 位置空出來放本次插入的數據,並將後面的數據向後移動一個位置。

其實擴容最終調用的代碼:數組

    private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; int newCapacity = oldCapacity + (oldCapacity >> 1); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); // minCapacity is usually close to size, so this is a win: elementData = Arrays.copyOf(elementData, newCapacity); }

也是一個數組複製的過程。安全

因而可知 ArrayList 的主要消耗是數組擴容以及在指定位置添加數據,在平常使用時最好是指定大小,儘可能減小擴容。更要減小在指定位置插入數據的操做。數據結構

序列化

因爲 ArrayList 是基於動態數組實現的,因此並非全部的空間都被使用。所以使用了 transient 修飾,能夠防止被自動序列化。併發

transient Object[] elementData;

所以 ArrayList 自定義了序列化與反序列化:dom

    private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException{ // Write out element count, and any hidden stuff int expectedModCount = modCount; s.defaultWriteObject(); // Write out size as capacity for behavioural compatibility with clone() s.writeInt(size); // Write out all elements in the proper order. //只序列化了被使用的數據 for (int i=0; i<size; i++) { s.writeObject(elementData[i]); } if (modCount != expectedModCount) { throw new ConcurrentModificationException(); } } private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { elementData = EMPTY_ELEMENTDATA; // Read in size, and any hidden stuff s.defaultReadObject(); // Read in capacity s.readInt(); // ignored if (size > 0) { // be like clone(), allocate array based upon size not capacity ensureCapacityInternal(size); Object[] a = elementData; // Read in all elements in the proper order. for (int i=0; i<size; i++) { a[i] = s.readObject(); } } }

當對象中自定義了 writeObject 和 readObject 方法時,JVM 會調用這兩個自定義方法來實現序列化與反序列化。

從實現中能夠看出 ArrayList 只序列化了被使用的數據。

以上爲複製git的內容。下面解釋一下其中我不太熟悉和明白的內容。

————————————————————————————————————————————————————————————

1、ArrayList

首先是ArrayList基礎,ArrayList指的就是動態數組,包括三個主要的優勢:

  動態的增長和減小元素 
  實現了ICollection和IList接口 
  靈活的設置數組的大小

ArrayList有三個構造器,分別是public ArrayList(); public ArrayList(ICollection); public ArrayList(int); (第一個構造器默認按照16的大小構造,第二個將該集合的元素添加到ArrayList,第三個按照參數構造)。

IsSynchronized屬性指示當前的ArrayList實例是否支持線程同步,而ArrayList.Synchronized靜態方法則會返回一個ArrayList的線程同步的封裝。 (先放這,線程問題後面再詳細解釋)

TrimSize方法用於將ArrayList固定到實際元素的大小,當動態數組元素肯定不在添加的時候,能夠調用這個方法來釋放空餘的內存。

效率問題:

1.數組擴容是對ArrayList效率影響比較大的一個因素。 每當執行Add、AddRange、Insert、InsertRange等添加元素的方法,都會檢查內部數組的容量是否不夠了,會以當前容量的兩倍來從新構建一個數組,將舊元素Copy到新數組中,而後丟棄舊數組。若是不運行TrimSize方法或者存儲元素不足填滿,會形成較大的內存浪費。

2.調用IndexOf、Contains等方法是執行的簡單的循環來查找元素,會形成效率低下的問題,還不如本身手寫算法進行查找。

2、List接口

List接口中能夠存聽任意的數據。並且在List接口中內容是容許重複的。List接口的功能要比Collection接口強大不少,由於大量擴充了Collection接口的操做。
擴展方法:
一、public void add(int index,E element)普通  在指定的位置增長元素。
二、public boolean addAll(int index, Collection<? extends E> c)  普通 在指定的位置增長一組元素。
三、E get(int index) 普通 返回指定位置的元素。
四、public int indexOf(Object o) 普通 查找指定元素的位置。
五、public int lastIndexOf(Object o) 普通 從後向前查找指定元素的位置。
六、public ListIterator<E> listIterator()普通 爲ListIterator接口實例化。
七、public E remove(int index) 普通 按指定的位置刪除元素。
八、public List<E> subList(int fromIndex, int toIndex) 普通 取出集合中的子集合。
九、public E set(int index, E element)普通 替換指定位置的元素。
 
3、RandomAccess接口
RandomAccess 是一個標記接口,用於標明實現該接口的List支持快速隨機訪問,主要目的是使算法可以在隨機和順序訪問的list中表現的更加高效。
 

Vector

Voctor 也是實現於 List 接口,底層數據結構和 ArrayList 相似,也是一個動態數組存放數據。不過是在 add() 方法的時候使用 synchronize 進行同步寫數據,可是開銷較大,因此 Vector 是一個同步容器並非一個併發容器。

如下是 add() 方法:

    public synchronized boolean add(E e) { modCount++; ensureCapacityHelper(elementCount + 1); elementData[elementCount++] = e; return true; }

以及指定位置插入數據:

    public void add(int index, E element) { insertElementAt(element, index); } public synchronized void insertElementAt(E obj, int index) { modCount++; if (index > elementCount) { throw new ArrayIndexOutOfBoundsException(index + " > " + elementCount); } ensureCapacityHelper(elementCount + 1); System.arraycopy(elementData, index, elementData, index + 1, elementCount - index); elementData[index] = obj; elementCount++; }
_________________________________________________________________________________
除此以外,再詳細介紹一個Vector這個神奇的東西。
Vector 是矢量隊列,它是JDK1.0版本添加的類。繼承於AbstractList,實現了List, RandomAccess, Cloneable這些接口。
Vector 繼承了AbstractList,實現了List;因此,它是一個隊列,支持相關的添加、刪除、修改、遍歷等功能
Vector 實現了RandmoAccess接口,即提供了隨機訪問功能。RandmoAccess是java中用來被List實現,爲List提供快速訪問功能的。在Vector中,咱們便可以經過元素的序號快速獲取元素對象;這就是快速隨機訪問。
Vector 實現了Cloneable接口,即實現clone()函數。它能被克隆。

和ArrayList不一樣,Vector中的操做是線程安全的
相關文章
相關標籤/搜索