天天學JAVA-JAVA基礎(7)-ArrayList

如果覺得我寫的還行,請關注我的博客並且點個贊喲。本文主要介紹JAVA 中最常用的集合ArrayList相關知識。

1.ArrayList簡介
1.1 .ArrayList簡介

1.ArrayList是基於動態數組實現的,數組具有按索引查找的特性,所以訪問很快,適合經常查詢的數據。其實就是對數組的操作。

2.ArrayList 繼承了AbstractList,實現了List。它是一個數組隊列,提供了相關的添加、刪除、修改、遍歷等功能。

3.ArrayList 實現了RandmoAccess接口,即提供了隨機訪問功能。RandmoAccess是java中用來被List實現,爲List提供快速訪問功能的。在ArrayList中,我們即可以通過元素的序號快速獲取元素對象;這就是快速隨機訪問。稍後,我們會比較List的“快速隨機訪問”和“通過Iterator迭代器訪問”的效率。

4.ArrayList 實現了Cloneable接口,即覆蓋了函數clone(),能被克隆。

5.ArrayList 實現java.io.Serializable接口,這意味着ArrayList支持序列化,能通過序列化去傳輸。

6.和Vector不同,ArrayList中的操作不是線程安全的!所以,建議在單線程中才使用ArrayList,而在多線程中可以選擇Vector或者CopyOnWriteArrayList。

1.2ArrayList關係圖
在這裏插入圖片描述
1.3.ArrayList 構造方法
在這裏插入圖片描述

1. 默認構造函數    
   ArrayList()

2. capacity是ArrayList的默認容量大小。當由於增加數據導致容量不足時,容量會添加上一次容量大小的一半。    
   ArrayList(int capacity)

3. 創建一個包含collection的ArrayList  
   ArrayList(Collection<? extends E> collection)

1.4 ArrayList API

// Collection中定義的API
boolean             add(E object)
boolean             addAll(Collection<? extends E> collection)
void                clear()
boolean             contains(Object object)
boolean             containsAll(Collection<?> collection)
boolean             equals(Object object)
int                 hashCode()
boolean             isEmpty()
Iterator<E>         iterator()
boolean             remove(Object object)
boolean             removeAll(Collection<?> collection)
boolean             retainAll(Collection<?> collection)
int                 size()
<T> T[]             toArray(T[] array)
Object[]            toArray()
// AbstractCollection中定義的API
void                add(int location, E object)
boolean             addAll(int location, Collection<? extends E> collection)
E                   get(int location)
int                 indexOf(Object object)
int                 lastIndexOf(Object object)
ListIterator<E>     listIterator(int location)
ListIterator<E>     listIterator()
E                   remove(int location)
E                   set(int location, E object)
List<E>             subList(int start, int end)
// ArrayList新增的API
Object               clone()
void                 ensureCapacity(int minimumCapacity)
void                 trimToSize()
void                 removeRange(int fromIndex, int toIndex)

1.5 ArrayList中的對象

    //默認初始長度.
    private static final int DEFAULT_CAPACITY = 10;

    //Shared empty array instance used for empty instances.
    private static final Object[] EMPTY_ELEMENTDATA = {};

    //
    private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};

   //動態數組
    transient Object[] elementData; // non-private to simplify nested class access

     //數組長度
    private int size;

ArrayList包含了兩個重要的對象:elementData 和 size。

(01) elementData是"Object[]類型的數組",它保存了添加到ArrayList中的元素。實際上,elementData是個動態數組,我們能通過構造函數
ArrayList(int initialCapacity)來執行它的初始容量爲initialCapacity;如果通過不含參數的構造函數ArrayList()來創建ArrayList,則elementData的容量默認是10。elementData數組的大小會根據ArrayList容量的增長而動態的增長,具體的增長方式,請參考源碼分析中的ensureCapacity()函數。

(02) size 則是動態數組的實際大小。

1.6 ArrayList 擴容

當ArrayList容量不足以容納全部元素時,ArrayList會重新設置容量:新的容量=“(原始容量x3)/2 + 1”。具體代碼如下

   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);
        // 擴容
        elementData = Arrays.copyOf(elementData, newCapacity);
    }

1.7 ArrayList 的遍歷

ArrayList支持的3種遍歷方式

(01) 第一種,通過迭代器遍歷。即通過Iterator去遍歷。

Integer value = null;
Iterator iter = list.iterator();
while (iter.hasNext()) {
    value = (Integer)iter.next();
}

(02) 第二種,隨機訪問,通過索引值去遍歷。 由於ArrayList實現了RandomAccess接口,它支持通過索引值去隨機訪問元素。

Integer value = null;
int size = list.size();
for (int i=0; i<size; i++) {
    value = (Integer)list.get(i);        
}

(03) 第三種,for循環遍歷。如下:

Integer value = null;
for (Integer integ:list) {
    value = integ;
}

如果你覺得我寫的還行,請關注我的博客並且點個贊喲,也請關注我的公衆號並加入下方QQ羣,每天都會定時推送乾貨以及共享一些優質的學習資料和視頻喲.
在這裏插入圖片描述
在這裏插入圖片描述

發佈了74 篇原創文章 · 獲贊 247 · 訪問量 29萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章