亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

ArrayList源碼解析(下)

2019-11-10 17:26:48
字體:
來源:轉載
供稿:網友

序列化操作,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(); } }}

迭代器

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

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. 實現了Iterator接口 2. cursor表示下一個將會返回的元素的下標,lastRet表示上當前元素的下標 3. fail-fast機制:

是java集合(Collection)中的一種錯誤機制。當多個線程對同一個集合的內容進行操作時,就可能會產生fail-fast事件。例如:當某一個線程A通過iterator去遍歷某集合的過程中,若該集合的內容被其他線程所改變了;那么線程A訪問集合時,就會拋出ConcurrentModificationException異常,產生fail-fast事件。

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

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(); } } // 在進行迭代操作時,如果同時又修改了elementData,則拋出異常 final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); }}

私有類ListItr 1. 實現了ListIterator接口 2. 實現過程和上面的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是原來List的一個視圖,也就是說對subList的所有操作會影響原有的List 小技巧:對list的某個區間內的數據全部刪除,一般的方法是做循環遍歷后刪掉,這里就可以利用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; } // 這個操作是針對原有List的elementData來的,其他如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(); }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
国产一区二区三区直播精品电影| 亚洲精品视频在线播放| 91精品国产电影| 欧美小视频在线| 欧美劲爆第一页| 91成人免费观看网站| 91亚洲国产精品| 亚洲午夜未满十八勿入免费观看全集| 欧美大片在线免费观看| 一区二区三区美女xx视频| 国产精品久久久久久超碰| 7777kkkk成人观看| 色哟哟网站入口亚洲精品| 欧美性受xxxx白人性爽| 久久在线视频在线| 91视频免费网站| 欧美激情在线播放| 欧美日韩国产中文字幕| 欧美性xxxxx| 国产精品视频999| 国产精品久久久久久久久久小说| 色天天综合狠狠色| 久久精品国产清自在天天线| 欧美在线观看网址综合| 亚洲黄色www网站| 日韩在线免费高清视频| 亚洲va国产va天堂va久久| 欧日韩在线观看| 不卡毛片在线看| 国产亚洲欧美视频| 日韩国产一区三区| 日韩电影中文字幕在线观看| 亚洲午夜激情免费视频| 欧美日韩国产精品一区| 久久夜色撩人精品| 亚洲人成电影网| 国产亚洲欧美视频| 国产精品一区二区久久精品| 国产视频精品免费播放| 成人激情视频在线| 国产精品自产拍在线观看| 欧美在线精品免播放器视频| 日韩在线欧美在线国产在线| 欧美xxxx18性欧美| 2019国产精品自在线拍国产不卡| 91中文在线观看| 欧美电影电视剧在线观看| 91精品国产自产在线观看永久| 亚洲在线观看视频网站| 亚洲精品99久久久久中文字幕| 亚洲人成五月天| 日韩精品极品视频| 欧美国产精品va在线观看| 成人欧美一区二区三区在线| 成人动漫网站在线观看| 亚洲人午夜精品| 91精品久久久久久久久久久久久| 欧美又大粗又爽又黄大片视频| 亚洲国产精品va在线观看黑人| 久久久精品一区二区三区| 亚洲色图18p| 久久久国产精彩视频美女艺术照福利| 欧美性videos高清精品| 亚洲国产91精品在线观看| 亚洲人高潮女人毛茸茸| 亚洲精品国产精品自产a区红杏吧| 久久成人国产精品| 亚洲一区中文字幕| 国产日韩中文字幕在线| 精品久久久久久久久国产字幕| 日韩精品在线私人| 国产精品视频播放| 欧美日韩激情网| 国产成人短视频| 亚洲男人天堂网| 2019中文字幕在线免费观看| 国产精品丝袜一区二区三区| 欧美一区二区.| 91成人在线播放| 成人久久久久久| 久久久久久久久网站| 亚洲热线99精品视频| 国产亚洲精品久久久久动| 97视频在线观看亚洲| 精品动漫一区二区| 亚洲综合精品伊人久久| 亚洲最大福利视频网站| 国产视频精品自拍| 国产精品劲爆视频| 国产日产久久高清欧美一区| 欧美性生交大片免费| 欧美日韩在线观看视频| 国产精品亚洲第一区| 美女视频黄免费的亚洲男人天堂| 日韩av网站导航| 欧美成人精品h版在线观看| 最近2019年好看中文字幕视频| 亚洲欧美激情四射在线日| 亚洲男人天天操| 欧美有码在线观看| 亚洲国产另类 国产精品国产免费| 欧美精品久久久久久久| 精品国产一区二区三区久久狼5月| 国产99久久精品一区二区永久免费| 久久综合色88| 久久精品国产欧美亚洲人人爽| 久久夜色精品国产欧美乱| 日韩在线观看电影| 亚洲欧美日韩视频一区| 91亚洲国产成人久久精品网站| 91丝袜美腿美女视频网站| 久久99精品国产99久久6尤物| 伊人久久久久久久久久久| 色综合久久天天综线观看| 日韩在线观看视频免费| 国产日本欧美视频| 一本一道久久a久久精品逆3p| 日韩欧美在线播放| 狠狠躁夜夜躁人人爽天天天天97| 日韩欧美综合在线视频| 一区二区欧美亚洲| 久久人人看视频| 色香阁99久久精品久久久| 日韩av在线一区二区| 九色91av视频| 深夜福利91大全| 日本在线观看天堂男亚洲| 美日韩精品免费视频| 日韩精品亚洲元码| 国产精品99久久久久久久久| 66m—66摸成人免费视频| 日产精品久久久一区二区福利| 欧美天堂在线观看| 日韩精品高清视频| 欧美另类99xxxxx| 国产日本欧美一区二区三区| 精品国产福利视频| 国产精品成人品| 国产成人一区二| 国外日韩电影在线观看| 亚洲国产成人在线播放| 欧美色xxxx| 久久99久久99精品中文字幕| 欧美日韩免费网站| 中文字幕综合在线| 亚洲新中文字幕| 奇米4444一区二区三区| 国产亚洲人成a一在线v站| 色哟哟入口国产精品| 日韩高清中文字幕| 国产亚洲欧美一区| 亚洲天堂男人天堂| 精品国内自产拍在线观看| 2021久久精品国产99国产精品| 欧美国产日韩中文字幕在线| 亚洲国产一区自拍| 国模视频一区二区三区| 亚洲国产成人av在线| 国产精品免费在线免费| 欧美精品videosex牲欧美| 成人免费午夜电影| 日韩的一区二区| 5566成人精品视频免费| 一区二区日韩精品|