/**
* Default initial capacity.
* 數(shù)組的默認(rèn)初始大小
*/
private static final int DEFAULT_CAPACITY = 10;
/**
*
*這里定義了兩個相同的object數(shù)組 區(qū)別是 DEFAULTCAPACITY_EMPTY_ELEMENTDATA數(shù)組知道擴(kuò)大多少當(dāng)?shù)谝粋€元素放入時
*
* Shared empty array instance used for empty instances.
*/
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.
*/
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
/**
* 數(shù)組緩沖區(qū) 存儲元素就是這個,他的大小就是ArrayList大小裂垦,每一個空的數(shù)組
* 當(dāng)elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA在插入第一個元素的時候就會擴(kuò)大到DEFAULT_CAPACITY
*
* 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
/**
* The size of the ArrayList (the number of elements it contains).
*
* @serial
*/
private int size;
/**
*
* 制定構(gòu)造初始大小
* 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
*/
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}
/**這里用的是 DEFAULTCAPACITY_EMPTY_ELEMENTDATA 說是初始為10 ensureCapacityInternal()在這里看
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
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;
}
}
/**
* 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); // Increments modCount!!
elementData[size++] = e;
return true;
}
/**
*當(dāng)ArrayList由ArrayList()構(gòu)造的時候 如果size小于10 最小容量設(shè)置為10
*
*/
private void ensureCapacityInternal(int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
ensureExplicitCapacity(minCapacity);
}
/*
*
* 判斷是否需要grow 當(dāng)最小容量大于ArrayList容量是時增長
*
*/
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
/**
* new = old + old /2
* old=size+1
* new=size+(size+1)/2
* new=1.5size;
* 新數(shù)組增長為原來的1.5倍
* 還判斷了超出了Integer 最大值
*
* 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);
}
/**
* ps:Arrays.copyOf()不僅僅只是拷貝數(shù)組中的元素,在拷貝元素時炭菌,會創(chuàng)建一個新的數(shù)組對象。而System.arrayCopy只拷貝已經(jīng)存在數(shù)組元素。
*remove 的時候是對自身進(jìn)行copy,沒有新的數(shù)組操作
* 然后GC執(zhí)行工作
* 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);
modCount++;
E oldValue = elementData(index);
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // clear to let GC do its work
return oldValue;
}
/**
*
*首先檢測是否越界
* 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)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
ArrayList 動態(tài)數(shù)組
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來芬位,“玉大人无拗,你說我怎么就攤上這事∶恋铮” “怎么了英染?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長被饿。 經(jīng)常有香客問我四康,道長,這世上最難降的妖魔是什么狭握? 我笑而不...
- 正文 為了忘掉前任闪金,我火速辦了婚禮,結(jié)果婚禮上论颅,老公的妹妹穿的比我還像新娘哎垦。我一直安慰自己,他們只是感情好恃疯,可當(dāng)我...
- 文/花漫 我一把揭開白布漏设。 她就那樣靜靜地躺著,像睡著了一般今妄。 火紅的嫁衣襯著肌膚如雪郑口。 梳的紋絲不亂的頭發(fā)上鸳碧,一...
- 文/蒼蘭香墨 我猛地睜開眼缸兔,長吁一口氣:“原來是場噩夢啊……” “哼日裙!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起惰蜜,我...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 年R本政府宣布,位于F島的核電站愤兵,受9級特大地震影響鹿霸,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜秆乳,卻給世界環(huán)境...
- 文/蒙蒙 一懦鼠、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧矫夷,春花似錦葛闷、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至忧陪,卻和暖如春扣泊,著一層夾襖步出監(jiān)牢的瞬間近范,已是汗流浹背。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- List ADT有兩種流行的實現(xiàn)方式蔗喂,Java中ArrayList類提供了可增長數(shù)組的實現(xiàn),LinkedList提...
- 集合中ArrayList高帖,一直認(rèn)為缰儿,擴(kuò)容的話,就是從新建了一個對象散址,然后把新對象的引用重新賦給當(dāng)前對象乖阵,最近想想不...
- 這篇文章主要討論Stack Overflow上面公選投票最多的一個問題:如何把下面的數(shù)組轉(zhuǎn)換成為一個ArrayLi...
- 在所有的瞪浸,我愛吃的蔬菜里頭,絲瓜可以說师崎,是我的最愛哦默终!絲瓜作為蔬菜椅棺,它營養(yǎng)價值超高:含蛋白質(zhì)犁罩、脂肪、碳水化合物两疚、磷...