麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

ArrayList源碼解析(中)

2019-11-10 20:24:41
字體:
供稿:網(wǎng)友

判斷元素位置

這些函數(shù)都相對簡單。因?yàn)榇鎯Φ脑乜赡転閚ull,所以判斷的時(shí)候多了一次。

public int size() { return size;}public boolean isEmpty() { return size == 0;}public boolean contains(Object o) { return indexOf(o) >= 0;}public int indexOf(Object o) { if (o == null) { for (int i = 0; i < size; i++) if (elementData[i]==null) return i; } else { for (int i = 0; i < size; i++) if (o.equals(elementData[i])) return i; } return -1;}public int lastIndexOf(Object o) { if (o == null) { for (int i = size-1; i >= 0; i--) if (elementData[i]==null) return i; } else { for (int i = size-1; i >= 0; i--) if (o.equals(elementData[i])) return i; } return -1;}

數(shù)組轉(zhuǎn)化

toArray()重載了兩個(gè)方法,其中一個(gè)返回Object[],另外一個(gè)返回指定類型的數(shù)組。對于有參的方法的調(diào)用建議:傳入一個(gè)空的對象,如 new Integer[]{} 作為參數(shù)。關(guān)于ClassCastException異常:經(jīng)常會需要將ArrayList里的內(nèi)容轉(zhuǎn)化為特定類型的數(shù)組,但是如果使用無參的toArray()進(jìn)行強(qiáng)制轉(zhuǎn)換,就會出現(xiàn)ClassCastException異常。此時(shí)需要使用第二個(gè)有參的方法。例如類似于: (Integer[]) ArrayList某個(gè)實(shí)例.toArray(new Integer[]{})。在我們使用 T[] toArray(T[] a) 的時(shí)候還需要再一次進(jìn)行顯式的轉(zhuǎn)化,這點(diǎn)有點(diǎn)不懂。因?yàn)檫@個(gè)函數(shù)內(nèi)部已經(jīng)轉(zhuǎn)化了,但是我們還需要 (Integer[]) ArrayList某個(gè)實(shí)例.toArray(new Integer[]{}) 這么寫,而不是 ArrayList某個(gè)實(shí)例.toArray(new Integer[]{}) 這么寫?依舊是有參的那個(gè)函數(shù),如果傳入的參數(shù)的長度大于size,得到的結(jié)果會變得沒有使用意義,正如下面代碼注釋里演示的一樣。public Object clone() { try { @Sup增刪改查// Positional access Operations@SuppressWarnings("unchecked")E elementData(int index) { return (E) elementData[index];}public E get(int index) { rangeCheck(index); return elementData(index);}public E set(int index, E element) { rangeCheck(index); E oldValue = elementData(index); elementData[index] = element; return oldValue;}add操作時(shí)怎么擴(kuò)充容量?調(diào)用ensureCapacityInternal(size + 1) 方法,如果 size+1 < elementData.length,則表示容量充足,不需要擴(kuò)充;如果反之,那么容量擴(kuò)充到原來的elementData.length 的1.5倍。在進(jìn)行add操作的時(shí)候,都會嘗試對elementData擴(kuò)充容量(ensureCapacityInternal()方法),這里有個(gè)提升效率的技巧,詳見ArrayList源碼解析(上)的 “關(guān)于擴(kuò)展容量的相關(guān)操作” 段落。/*** 每次進(jìn)行增加操作的時(shí)候,都會嘗試擴(kuò)充elementData的容量*/public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true;}public void add(int index, E element) { rangeCheckForAdd(index); ensureCapacityInternal(size + 1); // Increments modCount!! System.arraycopy(elementData, index, elementData, index + 1, size - index); elementData[index] = element; size++;}public boolean addAll(Collection<? extends E> c) { Object[] a = c.toArray(); int numNew = a.length; ensureCapacityInternal(size + numNew); // Increments modCount System.arraycopy(a, 0, elementData, size, numNew); size += numNew; return numNew != 0;}public boolean addAll(int index, Collection<? extends E> c) { rangeCheckForAdd(index); Object[] a = c.toArray(); int numNew = a.length; ensureCapacityInternal(size + numNew); // Increments modCount int numMoved = size - index; if (numMoved > 0) System.arraycopy(elementData, index, elementData, index + numNew, numMoved); System.arraycopy(a, 0, elementData, index, numNew); size += numNew; return numNew != 0;}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;}public boolean remove(Object o) { if (o == null) { for (int index = 0; index < size; index++) if (elementData[index] == null) { fastRemove(index); return true; } } else { for (int index = 0; index < size; index++) if (o.equals(elementData[index])) { fastRemove(index); return true; } } return false;}private void fastRemove(int index) { modCount++; 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}public void clear() { modCount++; // clear to let GC do its work for (int i = 0; i < size; i++) elementData[i] = null; size = 0;}protected void removeRange(int fromIndex, int toIndex) { modCount++; int numMoved = size - toIndex; System.arraycopy(elementData, toIndex, elementData, fromIndex, numMoved); // clear to let GC do its work int newSize = size - (toIndex-fromIndex); for (int i = newSize; i < size; i++) { elementData[i] = null; } size = newSize;}private void rangeCheck(int index) { if (index >= size) throw new IndexOutOfBoundsException(outOfBoundsMsg(index));}private void rangeCheckForAdd(int index) { if (index > size || index < 0) throw new IndexOutOfBoundsException(outOfBoundsMsg(index));}private String outOfBoundsMsg(int index) { return "Index: "+index+", Size: "+size;}public boolean removeAll(Collection<?> c) { return batchRemove(c, false);}public boolean retainAll(Collection<?> c) { return batchRemove(c, true);}

關(guān)于batchRemove()方法 1. 批量刪除的方法,具體是對集合c和elementData的交集處理,這里詳細(xì)說明一下。

假如集合c和elementData的交集是U,那么,如果complement是true,elementData最終會只存儲U;如果complement是false,elementData最終刪除U。

2. 在對elementData的元素進(jìn)行篩選的時(shí)候,這里使用了r、w兩個(gè)游標(biāo),從而避免從新開辟一個(gè)新的數(shù)組進(jìn)行存儲。這種方法也是比較常見的一種算法題。

private boolean batchRemove(Collection<?> c, boolean complement) { final Object[] elementData = this.elementData; int r = 0, w = 0; boolean modified = false; try { // 在原有數(shù)組上進(jìn)行篩選的方法,而不是另外開辟一個(gè)新的數(shù)組 for (; r < size; r++) if (c.contains(elementData[r]) == complement) elementData[w++] = elementData[r]; } finally { // Preserve behavioral compatibility with AbstractCollection, // even if c.contains() throws. if (r != size) { System.arraycopy(elementData, r, elementData, w, size - r); w += size - r; } if (w != size) { // clear to let GC do its work for (int i = w; i < size; i++) elementData[i] = null; modCount += size - w; size = w; modified = true; } } return modified;}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 久久婷婷一区二区三区 | 日本在线播放一区二区 | 久久国产精品久久久久久电车 | 久久久久久久久久久久免费 | 成人不卡在线观看 | 国产91丝袜在线播放 | 成年人在线视频免费 | 欧美一级美片在线观看免费 | 欧美性生活xxxxx | 麻豆国产一区 | 一级做a爱片久久毛片a高清 | 国产黄色录像片 | 蜜桃传免费看片www 一本色道精品久久一区二区三区 | 小视频免费在线观看 | 国产无遮挡一区二区三区毛片日本 | 日韩电影一区二区 | 欧美雌雄另类xxxxx | 最新精品在线 | a黄毛片 | 男女一边摸一边做羞羞视频免费 | 亚洲精品a在线观看 | 亚洲小视频在线播放 | chengrenyingshi| 91在线视频观看 | 久久成人福利 | 日本一道aⅴ不卡免费播放 久久久久久久高清 | 黄色网址你懂的 | 亚洲精品一区二区三区在线看 | 色污视频| 日本中文字幕网址 | 欧美日韩免费一区二区三区 | 亚洲精品欧美二区三区中文字幕 | 中文字幕免费在线观看视频 | 亚洲第一成人在线视频 | 天天透天天狠天天爱综合97 | 国产一区二区三区四区五区精品 | 国产精品久久久久久久午夜片 | av视在线 | 欧美a在线观看 | 在线成人看片 | xxxxxx性 |