(16) public synchronized void removeElementAt(int index)
源碼解釋:
獲取到index位置后有多少個(gè)元素,并將index位置后面的元素復(fù)制到index位置前的后面剃幌,再將index位置置空。復(fù)制操作通過(guò)JNI實(shí)現(xiàn)牍白。
public synchronized void removeElementAt(int index) {
modCount++;
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " +
elementCount);
}
else if (index < 0) {
throw new ArrayIndexOutOfBoundsException(index);
}
int j = elementCount - index - 1;// 獲取第index位置后有多少個(gè)元素
if (j > 0) {
System.arraycopy(elementData, index + 1, elementData, index, j);// 將emlementData[j, elementCount]復(fù)制到elementData[0, index]的后面
}
elementCount--;
elementData[elementCount] = null; // 等待GC
}
(17) public synchronized void insertElementAt(E obj, int index)
源碼解釋:
將index位置后的元素復(fù)制到原數(shù)組的index+1位置后的地方茂腥,并在index位置賦值obj切省。
public synchronized void insertElementAt(E obj, int index) {
modCount++;
if (index > elementCount) {
throw new ArrayIndexOutOfBoundsException(index
+ " > " + elementCount);
}
ensureCapacityHelper(elementCount + 1);// 判斷是否需要擴(kuò)容
System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);// 將elementData[index+1, elementCount]復(fù)制到elementData[index+1, elementCount +1]位置朝捆,
elementData[index] = obj;// 在index位置賦值obj
elementCount++;
}
(18) public synchronized void addElement(E obj)
源碼解釋:
在數(shù)組最后添加數(shù)據(jù)obj。先擴(kuò)容驯用,再通過(guò)數(shù)組賦值儒老。
public synchronized void addElement(E obj) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = obj;
}
(19) public synchronized boolean removeElement(Object obj)
源碼解釋:
移除元素驮樊。先獲取到移除元素在數(shù)組的位置,然后調(diào)用removeElementAt方法移除元素铝穷。
public synchronized boolean removeElement(Object obj) {
modCount++;
int i = indexOf(obj);
if (i >= 0) {
removeElementAt(i);
return true;
}
return false;
}
(20) public synchronized void removeAllElements()
源碼解釋:
移除所有元素佳魔。即將所有元素置為null鞠鲜,等待gc断国。
public synchronized void removeAllElements() {
modCount++;
// Let gc do its work
for (int i = 0; i < elementCount; i++)
elementData[i] = null;
elementCount = 0;
}
(21) public synchronized E get(int index)
源碼解釋:
獲取index位置的元素。
public synchronized E get(int index) {
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
return elementData(index);
}
(22) public synchronized E set(int index, E element)
源碼解釋:
修改index位置的值為element霞捡。實(shí)現(xiàn)原理也是直接數(shù)組賦值碧信。
public synchronized E set(int index, E element) {
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}
(23) public synchronized boolean add(E e)
源碼解釋:
在最后位置新增元素e。先判斷是否需要擴(kuò)容躏筏,然后賦值呈枉。實(shí)現(xiàn)原理和addElement是一樣的猖辫。
public synchronized boolean add(E e) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = e;
return true;
}
(24) public boolean remove(Object o)
源碼解釋:
移除元素,和removeElement方法一樣婶博。
public boolean remove(Object o) {
return removeElement(o);
}
(25) public void add(int index, E element)
源碼解釋:
添加元素荧飞,和insertElementAt方法一樣。
public void add(int index, E element) {
insertElementAt(element, index);
}
(26) public synchronized E remove(int index)
源碼解釋:
移除index位置的元素挠轴。實(shí)現(xiàn)思路和removeElementAt類似岸晦。
public synchronized E remove(int index) {
modCount++;
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
E oldValue = elementData(index);// 獲取到舊值
int numMoved = elementCount - index - 1;// index位置后面元素的個(gè)數(shù)
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index, numMoved);// 將elementData[index+1, elementCount]移到element[0, index]后
elementData[--elementCount] = null; // Let gc do its work
return oldValue;
}
(27) public void clear()
源碼解釋:
移除所有元素睛藻,直接調(diào)用removeAllElements()店印。
public void clear() {
removeAllElements();
}
(28) public synchronized boolean containsAll(Collection<?> c)
源碼解釋:
判斷是否包含集合c的所有元素。調(diào)用AbstractCollection的實(shí)現(xiàn),代碼也很簡(jiǎn)單,不贅敘。
public synchronized boolean containsAll(Collection c) {
return super.containsAll(c);
}
public boolean containsAll(Collection c) {
for (Object e : c)
if (!contains(e))
return false;
return true;
}
(29) public synchronized boolean addAll(Collection<? extends E> c)
源碼解釋:
把集合c復(fù)制到當(dāng)前數(shù)組的末尾侍郭。先判斷是否需要擴(kuò)容,然后調(diào)用JNI的arraycopy實(shí)現(xiàn)復(fù)制汰寓。
public synchronized boolean addAll(Collection c) {
modCount++;
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityHelper(elementCount + numNew);
System.arraycopy(a, 0, elementData, elementCount, numNew);
elementCount += numNew;
return numNew != 0;
}
(30) public synchronized boolean removeAll(Collection<?> c)
源碼解釋:
將包含集合c的所有元素移除苹粟。調(diào)用AbstractCollection的實(shí)現(xiàn)嵌削,代碼也很簡(jiǎn)單,不贅敘肌访。
public boolean removeAll(Collection c) {
Objects.requireNonNull(c);
boolean modified = false;
Iterator it = iterator();
while (it.hasNext()) {
if (c.contains(it.next())) {
it.remove();
modified = true;
}
}
return modified;
}
每天都在分享文章艇劫,也每天都有人想要我出來(lái)給大家分享下怎么去學(xué)習(xí)Java店煞。大家都知道,我們是學(xué)Java全棧的顷蟀,大家就肯定以為我有全套的Java系統(tǒng)教程鸣个。沒(méi)錯(cuò),我是有Java全套系統(tǒng)教程昼窗,進(jìn)扣裙【47】974【9726】所示涛舍,進(jìn)群的時(shí)候記得表明自己想要學(xué)習(xí)什么做盅,不要用小號(hào),這樣小編才好給你們發(fā)定向資源,今天小編就免費(fèi)送!~
“我們相信人人都可以成為一個(gè)程序員图筹,現(xiàn)在開(kāi)始,找個(gè)師兄扣溺,帶你入門(mén)瓜晤,學(xué)習(xí)的路上不再迷茫。這里是ja+va修真院驱犹,初學(xué)者轉(zhuǎn)行到互聯(lián)網(wǎng)行業(yè)的聚集地雄驹。"