国产激情自拍_国产9色视频_丁香花在线电影小说观看 _久久久久国产精品嫩草影院

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

ArrayList源碼解析(下)

2019-11-10 20:22:19
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

序列化操作,writeObject和readObject

PRivate void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException{ // Write out element count, and any hidden stuff int expectedModCount = modCount; s.defaultWriteObject(); // Write out size as capacity for behavioural compatibility with clone() s.writeInt(size); // Write out all elements in the proper order. for (int i=0; i<size; i++) { s.writeObject(elementData[i]); } if (modCount != expectedModCount) { throw new ConcurrentModificationException(); }}private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { elementData = EMPTY_ELEMENTDATA; // Read in size, and any hidden stuff s.defaultReadObject(); // Read in capacity s.readInt(); // ignored if (size > 0) { // be like clone(), allocate array based upon size not capacity ensureCapacityInternal(size); Object[] a = elementData; // Read in all elements in the proper order. for (int i=0; i<size; i++) { a[i] = s.readObject(); } }}

迭代器

三個(gè)和迭代器相關(guān)的API,分別為listIterator(int index)、listIterator()和iterator()。 迭代器相關(guān)參考Iterator和ListIterator對(duì)比

public ListIterator<E> listIterator(int index) { if (index < 0 || index > size) throw new IndexOutOfBoundsException("Index: "+index); return new ListItr(index);}public ListIterator<E> listIterator() { return new ListItr(0);}public Iterator<E> iterator() { return new Itr();}

私有類Itr 1. 實(shí)現(xiàn)了Iterator接口 2. cursor表示下一個(gè)將會(huì)返回的元素的下標(biāo),lastRet表示上當(dāng)前元素的下標(biāo) 3. fail-fast機(jī)制:

是java集合(Collection)中的一種錯(cuò)誤機(jī)制。當(dāng)多個(gè)線程對(duì)同一個(gè)集合的內(nèi)容進(jìn)行操作時(shí),就可能會(huì)產(chǎn)生fail-fast事件。例如:當(dāng)某一個(gè)線程A通過(guò)iterator去遍歷某集合的過(guò)程中,若該集合的內(nèi)容被其他線程所改變了;那么線程A訪問(wèn)集合時(shí),就會(huì)拋出ConcurrentModificationException異常,產(chǎn)生fail-fast事件。

這里的expectedModCount就是這也道理,具體在checkForComodification()這個(gè)方法里限制

private class Itr implements Iterator<E> { int cursor; // index of next element to return int lastRet = -1; // index of last element returned; -1 if no such int expectedModCount = modCount; public boolean hasNext() { return cursor != size; } @SuppressWarnings("unchecked") public E next() { checkForComodification(); int i = cursor; if (i >= size) throw new NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) throw new ConcurrentModificationException(); cursor = i + 1; return (E) elementData[lastRet = i]; } public void remove() { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { ArrayList.this.remove(lastRet); cursor = lastRet; lastRet = -1; //在上面remove方法后,保持modCount和expectedModCount一致 expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } // 在進(jìn)行迭代操作時(shí),如果同時(shí)又修改了elementData,則拋出異常 final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); }}

私有類ListItr 1. 實(shí)現(xiàn)了ListIterator接口 2. 實(shí)現(xiàn)過(guò)程和上面的Itr類很相似,就不在贅述。

private class ListItr extends Itr implements ListIterator<E> { ListItr(int index) { super(); cursor = index; } public boolean hasprevious() { return cursor != 0; } public int nextIndex() { return cursor; } public int previousIndex() { return cursor - 1; } @SuppressWarnings("unchecked") public E previous() { checkForComodification(); int i = cursor - 1; if (i < 0) throw new NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) throw new ConcurrentModificationException(); cursor = i; return (E) elementData[lastRet = i]; } public void set(E e) { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { ArrayList.this.set(lastRet, e); } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } public void add(E e) { checkForComodification(); try { int i = cursor; ArrayList.this.add(i, e); cursor = i + 1; lastRet = -1; expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } }}

截取List

截取的subList是原來(lái)List的一個(gè)視圖,也就是說(shuō)對(duì)subList的所有操作會(huì)影響原有的List 小技巧:對(duì)list的某個(gè)區(qū)間內(nèi)的數(shù)據(jù)全部刪除,一般的方法是做循環(huán)遍歷后刪掉,這里就可以利用subList快速刪除。例如: list.subList(200, 300).clear();

這里是API入口

public List<E> subList(int fromIndex, int toIndex) { subListRangeCheck(fromIndex, toIndex, size); return new SubList(this, 0, fromIndex, toIndex);}static void subListRangeCheck(int fromIndex, int toIndex, int size) { if (fromIndex < 0) throw new IndexOutOfBoundsException("fromIndex = " + fromIndex); if (toIndex > size) throw new IndexOutOfBoundsException("toIndex = " + toIndex); if (fromIndex > toIndex) throw new IllegalArgumentException("fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");}

私有的SubList類 和之前的代碼大同小異

private class SubList extends AbstractList<E> implements Randomaccess { private final AbstractList<E> parent; private final int parentOffset; private final int offset; int size; SubList(AbstractList<E> parent, int offset, int fromIndex, int toIndex) { this.parent = parent; this.parentOffset = fromIndex; this.offset = offset + fromIndex; this.size = toIndex - fromIndex; this.modCount = ArrayList.this.modCount; } // 這個(gè)操作是針對(duì)原有List的elementData來(lái)的,其他如add等操作也是如此 public E set(int index, E e) { rangeCheck(index); checkForComodification(); E oldValue = ArrayList.this.elementData(offset + index); ArrayList.this.elementData[offset + index] = e; return oldValue; } public E get(int index) { rangeCheck(index); checkForComodification(); return ArrayList.this.elementData(offset + index); } public int size() { checkForComodification(); return this.size; } public void add(int index, E e) { rangeCheckForAdd(index); checkForComodification(); parent.add(parentOffset + index, e); this.modCount = parent.modCount; this.size++; } public E remove(int index) { rangeCheck(index); checkForComodification(); E result = parent.remove(parentOffset + index); this.modCount = parent.modCount; this.size--; return result; } protected void removeRange(int fromIndex, int toIndex) { checkForComodification(); parent.removeRange(parentOffset + fromIndex, parentOffset + toIndex); this.modCount = parent.modCount; this.size -= toIndex - fromIndex; } public boolean addAll(Collection<? extends E> c) { return addAll(this.size, c); } public boolean addAll(int index, Collection<? extends E> c) { rangeCheckForAdd(index); int cSize = c.size(); if (cSize==0) return false; checkForComodification(); parent.addAll(parentOffset + index, c); this.modCount = parent.modCount; this.size += cSize; return true; } public Iterator<E> iterator() { return listIterator(); } public ListIterator<E> listIterator(final int index) { checkForComodification(); rangeCheckForAdd(index); final int offset = this.offset; return new ListIterator<E>() { int cursor = index; int lastRet = -1; int expectedModCount = ArrayList.this.modCount; public boolean hasNext() { return cursor != SubList.this.size; } @SuppressWarnings("unchecked") public E next() { checkForComodification(); int i = cursor; if (i >= SubList.this.size) throw new NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (offset + i >= elementData.length) throw new ConcurrentModificationException(); cursor = i + 1; return (E) elementData[offset + (lastRet = i)]; } public boolean hasPrevious() { return cursor != 0; } @SuppressWarnings("unchecked") public E previous() { checkForComodification(); int i = cursor - 1; if (i < 0) throw new NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (offset + i >= elementData.length) throw new ConcurrentModificationException(); cursor = i; return (E) elementData[offset + (lastRet = i)]; } public int nextIndex() { return cursor; } public int previousIndex() { return cursor - 1; } public void remove() { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { SubList.this.remove(lastRet); cursor = lastRet; lastRet = -1; expectedModCount = ArrayList.this.modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } public void set(E e) { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { ArrayList.this.set(offset + lastRet, e); } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } public void add(E e) { checkForComodification(); try { int i = cursor; SubList.this.add(i, e); cursor = i + 1; lastRet = -1; expectedModCount = ArrayList.this.modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } final void checkForComodification() { if (expectedModCount != ArrayList.this.modCount) throw new ConcurrentModificationException(); } }; } public List<E> subList(int fromIndex, int toIndex) { subListRangeCheck(fromIndex, toIndex, size); return new SubList(this, offset, fromIndex, toIndex); } private void rangeCheck(int index) { if (index < 0 || index >= this.size) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } private void rangeCheckForAdd(int index) { if (index < 0 || index > this.size) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } private String outOfBoundsMsg(int index) { return "Index: "+index+", Size: "+this.size; } private void checkForComodification() { if (ArrayList.this.modCount != this.modCount) throw new ConcurrentModificationException(); }}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
国产激情自拍_国产9色视频_丁香花在线电影小说观看 _久久久久国产精品嫩草影院
国产精品天堂| 国产成人天天5g影院| 99在线视频影院| 国产videos| 亚洲免费网站在线观看| 天天干天天摸| 尤物视频在线观看视频| 97国产视频| www.99av| 中文字幕在线视频网| 国产精品合集一区二区 | 免费一区二区三区视频狠狠| 欧洲有码在线视频| 国产视频三级在线观看播放| 99久久免费精品国产免费| 秋霞av在线| 国产精品冒白浆免费视频| 99免费视频| 国产精品天堂| 亚洲精品国自产拍在线观看| a视频在线观看| 五月婷婷导航| jizz亚洲| 精品国产免费观看一区| 九九热在线观看视频| 中文国产字幕在线观看| 久久精品最新免费国产成人| 国产不卡在线| 中文字幕第一页在线| 青草视频在线播放| 精品网站www| 国产天堂在线观看| 国产在线高清| h网址在线观看| 影音av资源站| 国产欧美日韩精品综合| 免费看黄视频网站| a√在线视频| 69国产精品| 国产福利小视频在线观看| 天堂资源中文在线| 日本中文字幕在线视频| 国产美女极品在线| 成人福利视频导航| 国产女呦网站| 国产www在线观看| 国产中文第一页| gogo高清在线播放免费| www.狠狠色.com| 美女永久在线网站| 黄色一级片视频| 免费在线观看a| 国产中文在线视频| 国产麻豆精品高清在线播放| 成年人在线观看| 国产精品爱久久久久久久小说| 国产区在线观看| 影音先锋中文字幕在线| 天堂中文在线观看| 久久国产热视频| 中文字幕第一页av| 中文字幕久热在线精品| 国产三级视频| 亚洲第一成人在线视频| 国产永久免费| 精品剧情v国产在线观看| 亚洲字幕成人中文在线观看| 日本在线视频www鲁啊鲁| 国产精品作爱| 精品久久久久一区二区三区| а√最新版地址在线天堂| 国产精品臀控福利在线观看| 91美女主播在线视频| sm国产在线调教视频| 国产精品爱久久久久久久小说| 羞羞视频在线免费看| 日本福利在线| 国产精品剧情一区二区在线观看| 亚洲一区免费在线| 国产污污在线观看| 麻豆电影传媒二区| 在线免费国产视频| 国产一区二区三区不卡在线| 丝袜理论片在线观看| 国产精品伦一区二区三区级视频频 | 青青草在线播放| 国产乱视频在线观看| 91福利在线视频| 国产精美视频| 天堂在线看视频| 免费特级黄毛片| 亚洲视频精品在线观看| 国产人成精品| 国产一区二区三区四区尤物| 日本不卡1区2区3区| 在线免费看黄av| 精精国产xxxx视频在线动漫| 国产欧美黑人| 国产女王在线**视频 | 国产porny蝌蚪视频| 国产美女视频一区二区二三区| 奇米影视狠狠狠| 女子免费在线观看视频www| 四虎一区二区三区| v天堂福利视频在线观看| 四虎国产精品永久地址998| 99reav| 亚洲精品影视在线| www.狠狠色.com| 男人操女人免费网站| 欧美精品日韩少妇| 激情网站在线| 日本中文字幕在线2020| 国产日产一区二区| 国产国产人免费人成免费视频| 国产精品免费视频一区一| 国产午夜精品一区理论片| 国产精品白浆流出视频| 99精品老司机免费视频| 日本啊v在线| 资源视频在线播放免费| 国产精品剧情一区二区在线观看 | 在线一区观看| 亚洲精品男人| 国产乱妇乱子| 精品入口蜜桃| 国产农村av| 在线观看免费黄色| 国内精品免费一区二区三区| 国产日韩欧美精品一区二区三区 | 中文在线官网天堂| 阿v免费在线观看| 国产精品xxx电影| 日p在线观看| 91极品在线| аⅴ成人天堂中文在线| 久久亚洲国产成人亚| 国产羞羞视频| av在线首页| 国产精品视频福利一区二区| av网址在线免费观看| 天天操天天艹| 欧美卡一卡二| 精品极品三级久久久久| 另类综合图区| av一级在线| 国产福利在线播放麻豆| 国产美女免费观看| www.91在线播放| 精品全国在线一区二区| 最新中文字幕av专区| 免费精品国产自产拍在| av一本在线| 91午夜视频| 18激情网站| 国产区卡一卡二卡三乱码免费| 青青草视频免费在线观看| 在线观看的av| 亚洲xxxxxx| 国产日韩欧美第一页| 超碰免费在线| 老师我好爽再深一点的视频| 在线激情小视频| 中文资源在线网| 国产成免费视频| 国产精品欧美色图| 国产精品白浆视频免费观看| 国产视频xxxx| 69日小视频在线观看| 2021av天天| 99爱在线观看| h网站免费在线观看| 日韩在线天堂| 国产在线观看a视频| 男女羞羞视频在线观看| 免费99热在线观看| 国产美女免费观看| 国产精品bbw一区二区三区| 2018中文字幕在线| 日本成a人片在线观看| 超碰在线国产| 国产一级免费在线观看| 伊人中文字幕在线| 丁香花在线电影| 国产精品69xx| 青青草免费观看免费视频在线| 成在线人视频免费视频| 国产精品毛片一区二区三区四区| 国产区在线观看| 伊人免费在线| 国产专区在线播放| 国产www视频在线观看| wwwww在线观看免费视频| 国产乱精品一区二区三区| 国产日本在线观看| 激情网站在线| 在线播放www| 国产精品被窝福利一区| 久久久久久久久免费视频| 91在线看片|