Java 集合系列(二)ArrayList詳解
ArrayList
ArrayList 是通過(guò)一個(gè)數(shù)組來(lái)實(shí)現(xiàn)的,因此它是在連續(xù)的存儲(chǔ)位置存放對(duì)象的引用,只不過(guò)它比 Array 更智能,能夠根據(jù)集合長(zhǎng)度進(jìn)行自動(dòng)擴(kuò)容。
假設(shè)讓我們來(lái)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的能夠自動(dòng)擴(kuò)容的數(shù)組,我們最容易想到的點(diǎn)就是:
- add()的時(shí)候需要判斷當(dāng)前數(shù)組size+1是否等于此時(shí)定義的數(shù)組大?。?/li>
- 若小于直接添加即可;否則,需要先擴(kuò)容再進(jìn)行添加。
實(shí)際上,ArrayList的內(nèi)部實(shí)現(xiàn)原理也是這樣子,我們可以來(lái)研究分析一下ArrayList的源碼
add(E e) 源碼分析
/**
* Appends the specified element to the end of this list.
*
* @param e element to be appended to this list
* @return <tt>true</tt> (as specified by {@link Collection#add})
*/
public boolean add(E e) {
ensureCapacityInternal(size + 1); // 進(jìn)行擴(kuò)容校驗(yàn)
elementData[size++] = e; // 將值添加到數(shù)組后面,并將 size+1
return true;
}
/**
* 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
private void ensureCapacityInternal(int minCapacity) {
ensureExplicitCapacity(calculateCapacity(elementData, minCapacity)); // elementData 數(shù)組
}
/**
* Default initial capacity.
*/
private static final int DEFAULT_CAPACITY = 10;
/**
* 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 = {};
// 返回最大的 index
private static int calculateCapacity(Object[] elementData, int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { // 與空數(shù)組實(shí)例對(duì)比
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
return minCapacity;
}
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
擴(kuò)容調(diào)用方法,實(shí)際也就是數(shù)組復(fù)制的過(guò)程
/**
* Increases the capacity to ensure that it can hold at least the
* number of elements specified by the minimum capacity argument.
*
* @param minCapacity the desired minimum capacity
*/
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);
}
add(int index, E element) 源碼分析
/**
* Inserts the specified element at the specified position in this
* list. Shifts the element currently at that position (if any) and
* any subsequent elements to the right (adds one to their indices).
*
* @param index index at which the specified element is to be inserted
* @param element element to be inserted
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public void add(int index, E element) {
rangeCheckForAdd(index); // 校驗(yàn)index是否超過(guò)當(dāng)前定義的數(shù)組大小范圍,超過(guò)則拋出 IndexOutOfBoundsException
ensureCapacityInternal(size + 1); // Increments modCount!!
System.arraycopy(elementData, index, elementData, index + 1,
size - index); // 復(fù)制,向后移動(dòng)
elementData[index] = element;
size++;
}
/**
* A version of rangeCheck used by add and addAll.
*/
private void rangeCheckForAdd(int index) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
從上面的源碼分析可知,擴(kuò)容和隨機(jī)插入元素的消耗比較大,因此在實(shí)際開(kāi)發(fā)中,應(yīng)盡量指定ArrayList大小,減少在隨機(jī)插入操作。
優(yōu)缺點(diǎn)
優(yōu)點(diǎn)
- 封裝了一個(gè)動(dòng)態(tài)再分配的對(duì)象數(shù)組
- 使用索引進(jìn)行隨機(jī)訪問(wèn)效率高
缺陷
- 在數(shù)組中增刪一個(gè)元素,所有元素都要往后往前移動(dòng),效率低下
知識(shí)腦圖

以上所述是小編給大家介紹的Java集合系列ArrayList詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- java ArrayList集合中的某個(gè)對(duì)象屬性進(jìn)行排序的實(shí)現(xiàn)代碼
- java集合類(lèi)arraylist循環(huán)中刪除特定元素的方法
- java 集合之實(shí)現(xiàn)類(lèi)ArrayList和LinkedList的方法
- Java集合刪除元素ArrayList實(shí)例詳解
- Java集合框架ArrayList源碼分析(一)
- Java集合系列之ArrayList源碼分析
- Java concurrency集合之 CopyOnWriteArrayList_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
- Java中ArrayList集合的常用方法大全
相關(guān)文章
基于springboot?配置文件context-path的坑
這篇文章主要介紹了基于springboot?配置文件context-path的坑,基于很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
SpringBoot實(shí)現(xiàn)elasticsearch 查詢(xún)操作(RestHighLevelClient 
這篇文章主要給大家介紹了SpringBoot如何實(shí)現(xiàn)elasticsearch 查詢(xún)操作,文中有詳細(xì)的代碼示例和操作流程,具有一定的參考價(jià)值,需要的朋友可以參考下2023-07-07
一文帶你了解Spring的Bean初始化過(guò)程和生命周期
Spring的核心功能有三點(diǎn)IOC、DI、AOP,IOC則是基礎(chǔ),也是Spring功能的最核心的點(diǎn)之一。今天一起來(lái)總結(jié)下Spring中Bean是怎么被創(chuàng)建出來(lái)的2023-03-03
Java通過(guò)值查找對(duì)應(yīng)的枚舉的實(shí)現(xiàn)
本文主要介紹了Java通過(guò)值查找對(duì)應(yīng)的枚舉的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02
Java中幾個(gè)Reference常見(jiàn)的作用詳解
這篇文章主要給大家介紹了Java中關(guān)于Reference多個(gè)作用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。2017-06-06
關(guān)于Java語(yǔ)法糖以及語(yǔ)法糖的原理和用法
這篇文章主要介紹了關(guān)于Java什么是語(yǔ)法糖以及語(yǔ)法糖的種類(lèi),也稱(chēng)糖衣語(yǔ)法,是由英國(guó)計(jì)算機(jī)學(xué)家?Peter.J.Landin?發(fā)明的一個(gè)術(shù)語(yǔ),指在計(jì)算機(jī)語(yǔ)言中添加的某種語(yǔ)法,這種語(yǔ)法對(duì)語(yǔ)言的功能并沒(méi)有影響,但是更方便程序員使用,需要的朋友可以參考下2023-05-05
解析SpringBoot中使用LoadTimeWeaving技術(shù)實(shí)現(xiàn)AOP功能
這篇文章主要介紹了SpringBoot中使用LoadTimeWeaving技術(shù)實(shí)現(xiàn)AOP功能,AOP面向切面編程,通過(guò)為目標(biāo)類(lèi)織入切面的方式,實(shí)現(xiàn)對(duì)目標(biāo)類(lèi)功能的增強(qiáng),本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
深入學(xué)習(xí)Java單元測(cè)試(Junit+Mock+代碼覆蓋率)
在做單元測(cè)試時(shí),代碼覆蓋率常常被拿來(lái)作為衡量測(cè)試好壞的指標(biāo),甚至,用代碼覆蓋率來(lái)考核測(cè)試任務(wù)完成情況,比如,代碼覆蓋率必須達(dá)到80%或 90%。下面我們就來(lái)詳細(xì)學(xué)習(xí)下java單元測(cè)試吧2019-06-06

