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

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

ArrayList源碼解析(下)

2019-11-10 18:10:54
字體:
來源:轉載
供稿:網友

序列化操作,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
久久国产精彩视频| 久久躁狠狠躁夜夜爽| 国产精品r级在线| 欧美性高跟鞋xxxxhd| 亚洲第一综合天堂另类专| 国产91在线播放| 久久久久国产精品免费| 亚洲白拍色综合图区| 精品久久久999| 国产亚洲精品va在线观看| 国产乱肥老妇国产一区二| 欧美wwwxxxx| 久久久精品国产网站| 国产精品久久久久久久久男| 欧美极品在线视频| 亚洲美女精品久久| 性色av一区二区三区免费| 性色av一区二区三区免费| 九色91av视频| 亚洲男人第一网站| 亚洲成色999久久网站| 亚洲男人av电影| 欧美韩国理论所午夜片917电影| 欧美日韩日本国产| 国产丝袜一区二区三区免费视频| 成人精品视频99在线观看免费| 91精品国产乱码久久久久久蜜臀| 91久久久精品| 菠萝蜜影院一区二区免费| 91产国在线观看动作片喷水| 中文字幕欧美亚洲| 中文字幕日本欧美| 欧美激情啊啊啊| 欧美中文在线视频| 亚洲天堂av图片| 日韩视频中文字幕| 日韩精品中文字幕在线播放| 亚洲美女av黄| 2019中文字幕在线观看| 欧美老女人www| 69国产精品成人在线播放| 亚洲电影免费观看高清完整版| 国产精品久久久久9999| 日韩有码片在线观看| 日韩欧美在线免费| 国产suv精品一区二区三区88区| 亚洲一区二区三区sesese| 91久久在线观看| 国产成人精品亚洲精品| 日韩精品中文在线观看| 国产69精品久久久久99| 欧美在线一区二区三区四| 国产99久久久欧美黑人| 国产精品久久久久久久午夜| 国内精品模特av私拍在线观看| 亚洲视频日韩精品| 欧洲中文字幕国产精品| 午夜精品一区二区三区视频免费看| 国产日韩中文字幕| 精品视频在线导航| 精品久久香蕉国产线看观看gif| 91精品国产色综合久久不卡98| www.日韩免费| 亚洲视频电影图片偷拍一区| 亚洲深夜福利视频| 一区二区亚洲精品国产| 久久久久久噜噜噜久久久精品| 成人欧美一区二区三区在线| 中文日韩在线视频| 韩日欧美一区二区| 精品福利一区二区| 久久久久久久激情视频| 久久久久久久久91| 欧美激情一区二区三区久久久| 日韩亚洲欧美中文在线| 国产精国产精品| 中文字幕欧美日韩va免费视频| 亚洲色图激情小说| 国产精品视频免费观看www| 91午夜理伦私人影院| 久久久久久高潮国产精品视| 日韩在线观看av| 欧美高清电影在线看| 欧美日韩福利电影| 国产精品吴梦梦| 欧美激情一区二区三区在线视频观看| 91美女高潮出水| 亚洲免费视频网站| 久久精品亚洲热| 欧美巨大黑人极品精男| 精品久久久av| 日本精品视频在线播放| 国产精品久久久久久久久影视| 亚洲成人久久一区| 伊人伊成久久人综合网小说| 深夜福利亚洲导航| 欧美尺度大的性做爰视频| 久久精品国产99国产精品澳门| 九九综合九九综合| 欧美怡春院一区二区三区| 日韩女优在线播放| 国产精品啪视频| 亚洲专区中文字幕| 日本精品性网站在线观看| 3344国产精品免费看| 亚洲国产精品悠悠久久琪琪| 国产日韩欧美一二三区| 国产一区二区激情| 萌白酱国产一区二区| 日韩精品欧美激情| 中文字幕久精品免费视频| 成人免费淫片aa视频免费| 欧美激情一区二区三区在线视频观看| 久久久99久久精品女同性| 日韩av中文字幕在线免费观看| 97人洗澡人人免费公开视频碰碰碰| 欧美激情videoshd| 日本高清+成人网在线观看| 最近2019中文字幕大全第二页| 91精品久久久久久久久久久久久| 久久久极品av| 亚洲a一级视频| 日本人成精品视频在线| 国产日韩精品电影| 亚洲国内高清视频| 亚洲奶大毛多的老太婆| 国产日韩精品在线| 欧美日韩在线观看视频| 精品日韩中文字幕| 久久久亚洲精选| 韩日精品中文字幕| 亚洲va欧美va在线观看| 国产精品高清免费在线观看| 日韩精品有码在线观看| 日韩av中文在线| 91久久精品久久国产性色也91| 欧美一级视频免费在线观看| 国产99久久精品一区二区永久免费| 国产成人欧美在线观看| 久久久久久中文字幕| 国产成人精品最新| 欧美色道久久88综合亚洲精品| 日韩一区视频在线| 日韩电影在线观看永久视频免费网站| 一区二区三区四区在线观看视频| 2019中文字幕免费视频| 欧美午夜无遮挡| 欧美一性一乱一交一视频| 亚洲男人天天操| 免费不卡欧美自拍视频| 久久国产精品99国产精| 国产一区二区三区在线观看网站| 免费av在线一区| 国产精品成人品| 国产精品久久久久国产a级| 性欧美激情精品| 欧美激情一区二区三区成人| 国产精品视频成人| 亚洲男人7777| 欧美国产中文字幕| 精品动漫一区二区| 456亚洲影院| 日韩一区二区三区国产| 91大神在线播放精品|