日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

java arraylist 源代码_Java中ArrayList源码浅析

發(fā)布時間:2025/3/20 java 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java arraylist 源代码_Java中ArrayList源码浅析 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

ArrayList基本使用

public class ArrayListTest {

public static void main(String[] args) {

List list = new ArrayList<>();

list.add("5");

System.out.println(list.get(0));

}

}

ArrayList繼承層次

public class ArrayList extends AbstractList

implements List, RandomAccess, Cloneable, java.io.Serializable

基本字段

private static final long serialVersionUID = 8683452581122892189L;

/**

* Default initial capacity.

*/

//默認(rèn)的初始化容量

private static final int DEFAULT_CAPACITY = 10;

/**

* Shared empty array instance used for empty instances.

*/

//空數(shù)組,會在構(gòu)造函數(shù)中給予0參數(shù)的情況下,賦值給elementData

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.

*/

//在一個個元素加入進來的時候,會自動擴展成為DEFAULTCAPACITY

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.

*/

//真正的數(shù)組的引用

transient Object[] elementData; // non-private to simplify nested class access

/**

* The size of the ArrayList (the number of elements it contains).

*

* @serial

*/

//添加的元素數(shù)量

private int size;

/**

* The maximum size of array to allocate.

* Some VMs reserve some header words in an array.

* Attempts to allocate larger arrays may result in

* OutOfMemoryError: Requested array size exceeds VM limit

*/

//是因為有header words?才導(dǎo)致要Integer的最大值減8

//其實這個變量只在后面使用一次,比較了之后,然后還是按照MAX_VALUE來擴容了...為啥

private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

構(gòu)造函數(shù)

/**

* 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

*/

//帶有初始化容量的構(gòu)造函數(shù)

public ArrayList(int initialCapacity) {

if (initialCapacity > 0) {

//初始化一個initialCapacity大小的Object數(shù)組

this.elementData = new Object[initialCapacity];

} else if (initialCapacity == 0) {

//若初始化為0,則使用默認(rèn)的空數(shù)組賦值

this.elementData = EMPTY_ELEMENTDATA;

} else {

//若為負(fù)數(shù),拋出非法參數(shù)異常

throw new IllegalArgumentException("Illegal Capacity: "+

initialCapacity);

}

}

/**

* Constructs an empty list with an initial capacity of ten.

*/

//最基本的構(gòu)造函數(shù)

public ArrayList() {

//注意這里被賦值為DEFAULTCAPACITY_EMPTY_ELEMENTDATA

//而不是EMPTY_ELEMENTDATA,表示的意思就是當(dāng)add的時候

//會默認(rèn)擴容為DEFAULT_CAPACITY

this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;

}

/**

* Constructs a list containing the elements of the specified

* collection, in the order they are returned by the collection's

* iterator.

*

* @param c the collection whose elements are to be placed into this list

* @throws NullPointerException if the specified collection is null

*/

public ArrayList(Collection extends E> c) {

elementData = c.toArray();

if ((size = elementData.length) != 0) {

// c.toArray might (incorrectly) not return Object[] (see 6260652)

if (elementData.getClass() != Object[].class)

elementData = Arrays.copyOf(elementData, size, Object[].class);

} else {

// replace with empty array.

this.elementData = EMPTY_ELEMENTDATA;

}

}

add操作

/**

* Appends the specified element to the end of this list.

*

* @param e element to be appended to this list

* @return true (as specified by {@link Collection#add})

*/

public boolean add(E e) {

//添加元素的時候,調(diào)用內(nèi)部確保容量的方法

//為什么是內(nèi)部呢?因為還有一個共有的ensureCapacity方法

ensureCapacityInternal(size + 1); // Increments modCount!!

//因為本身是一個數(shù)組,所以在下一個數(shù)組索引位置添加上元素

elementData[size++] = e;

return true;

}

private void ensureCapacityInternal(int minCapacity) {

//如果elementDate等于DEFAULTCAPACITY_EMPTY_ELEMENTDATA

//表示第一次擴容時,起碼要擴容至DEFAULT_CAPACITY

if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {

//取DEFAULT_CAPACITY=10和minCapacity(可以在初始化函數(shù)中初始化)的最大值

//也就是說若調(diào)用默認(rèn)構(gòu)造函數(shù),第一次會起碼擴展為10的大小

minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);

}

//確保顯示的容量

ensureExplicitCapacity(minCapacity);

}

其實ensureCapacityInternal一個任務(wù)是算出不論是第一次添加導(dǎo)致的擴容還是后面添加導(dǎo)致的擴容的最小的容量值。然后將這個最小擴容值傳遞給ensureExplicitCapacity,由ensureExplicitCapacity實現(xiàn)擴容。

private void ensureExplicitCapacity(int minCapacity) {

//用于迭代器的fail fast機制

modCount++;

// overflow-conscious code

//如果最小容量大于數(shù)組的長度,那么擴容。

//否則,則不擴容。

if (minCapacity - elementData.length > 0)

grow(minCapacity);

}

/**

* 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;

//新的容量為舊的容量的1.5倍

int newCapacity = oldCapacity + (oldCapacity >> 1);

//如果擴了1.5倍之后,還是小,那么以最小的容量進行擴容

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擴展為newCapacity大小,使用復(fù)制數(shù)組的方式

elementData = Arrays.copyOf(elementData, newCapacity);

}

private static int hugeCapacity(int minCapacity) {

//如果minCapacity小于0(而minCapacity是由某數(shù)+1得到)

//其實也剛開始想錯了,minCapacity也有可能是addAll()調(diào)用導(dǎo)致的

//反正minCapacity是一個需要保證的最小的容量值,不需要去理解

//其他函數(shù)是如何調(diào)用的

//所以minCapacity是由于Integer溢出的

if (minCapacity < 0) // overflow

throw new OutOfMemoryError();

//這里表示將最大容量擴展為Integer.MAX_VALUE,那么MAX_ARRAY_SIZE還有什么意義呢?

//這里minCapacity沒有溢出說明是小于MAX_ARRAY_SIZE,但是分為兩種情況,如果小于MAX_ARRAY_SIZE

//那么就直接擴容為MAX_ARRAY_SIZE

//否則比如說是MAX_VALUE-2,那么最多擴容為MAX_VALUE

return (minCapacity > MAX_ARRAY_SIZE) ?

Integer.MAX_VALUE :

MAX_ARRAY_SIZE;

}

get操作

/**

* Returns the element at the specified position in this list.

*

* @param index index of the element to return

* @return the element at the specified position in this list

* @throws IndexOutOfBoundsException {@inheritDoc}

*/

public E get(int index) {

//判斷是否越界

rangeCheck(index);

//返回數(shù)組的值

return elementData(index);

}

/**

* Checks if the given index is in range. If not, throws an appropriate

* runtime exception. This method does *not* check if the index is

* negative: It is always used immediately prior to an array access,

* which throws an ArrayIndexOutOfBoundsException if index is negative.

*/

private void rangeCheck(int index) {

if (index >= size)

//如果下標(biāo)大于元素的數(shù)量,那么拋出異常

throw new IndexOutOfBoundsException(outOfBoundsMsg(index));

}

remove操作

/**

* Removes the element at the specified position in this list.

* Shifts any subsequent elements to the left (subtracts one from their

* indices).

*

* @param index the index of the element to be removed

* @return the element that was removed from the list

* @throws IndexOutOfBoundsException {@inheritDoc}

*/

public E remove(int index) {

rangeCheck(index);

//add和remove都需要讓modCount加一

modCount++;

E oldValue = elementData(index);

int numMoved = size - index - 1;

if (numMoved > 0)

System.arraycopy(elementData, index+1, elementData, index,

numMoved);

//最大位的引用置為null,讓GC回收

elementData[--size] = null; // clear to let GC do its work

//返回刪除的值

return oldValue;

}

參考

總結(jié)

以上是生活随笔為你收集整理的java arraylist 源代码_Java中ArrayList源码浅析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 噜噜噜亚洲色成人网站 | 女生喷水视频 | 丁香花电影在线观看免费高清 | 青青操视频在线 | 欧美亚日韩 | 日本欧美韩国国产精品 | 91色九色| 骚五月| 激情久久av一区av二区av三区 | jizz中国女人高潮 | 第四色影音先锋 | 国产成人精品亚洲男人的天堂 | 男人午夜天堂 | www操操操| 黄色免费在线播放 | 都市激情中文字幕 | 九色综合网 | 天堂网中文在线观看 | 欧美大片在线看 | 在线播放91 | 欧美激情一级精品国产 | 国产黄色精品网站 | 六月丁香av| 黄瓜视频在线观看污 | 蜜桃视频一区二区三区 | 少妇又紧又色又爽又刺激视频 | 欧美情爱视频 | 亚洲草草网 | 精品成人中文无码专区 | 国产精品无遮挡 | 欧美激情视频在线 | 新婚若妻侵犯中文字幕 | 亚洲 高清 成人 动漫 | 中文字幕第三页 | 林天顾悦瑶笔趣阁 | 亚洲做受高潮无遮挡 | 爱情岛亚洲首页论坛小巨 | 久久久久久久国产精品视频 | 伊人久久爱 | 99免费精品视频 | 欧美午夜性 | 新狠狠干 | av免费福利 | 国产99在线视频 | 逼逼爱插插网站 | 日韩人体视频 | 日本免费黄色大片 | 国产精品www色诱视频 | 精品少妇av | 又色又爽又高潮免费视频国产 | 欧美黑人啪啪 | 国产三级av在线播放 | 操操操网站| 久久精品视频网站 | 性自由色xxxx免费视频 | 中国女人和老外的毛片 | 欧美综合网站 | 91www在线观看 | a级在线视频 | 免费日韩在线 | 成人欧美一区二区三区小说 | 日韩一级片免费 | 精品国产免费人成在线观看 | 国产一级黄色录像 | 亚洲国产综合av | 日韩三级在线观看 | 男生操女生逼逼 | 黄色av网址在线观看 | 992tv成人免费视频 | 欧美人妻一区二区 | 国产精品一区二区三区四区视频 | 91精品人妻一区二区三区果冻 | 美女被娇喘流出白 | 免费一级片视频 | 欧美涩涩视频 | 原创少妇半推半就88av | 成人依依网 | 就操网| 黄色国产大片 | 久久这里都是精品 | 日韩欧美一区二区视频 | 色综合久久五月 | 亚洲国产精品区 | 国产a精品| 免费成年人视频 | 国产亚洲美女精品久久久2020 | 深夜福利视频网站 | 伊人久久免费 | 偷拍视频一区 | 第一章激情艳妇 | 青娱乐国产在线 | 伊人成人在线 | 亚洲美女自拍偷拍 | 亚洲精品久久久狠狠狠爱 | 欧美色性视频 | 欧洲免费av | 欧美成人一区二区视频 | 蜜臀尤物一区二区三区直播 | 成人免费无遮挡无码黄漫视频 |