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

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

ArrayList源碼解析(下)

2019-11-10 20:22:19
字體:
來源:轉載
供稿:網友

序列化操作,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
日韩欧美在线观看| 欧美精品www在线观看| 96国产粉嫩美女| 亚洲综合最新在线| 日韩欧美在线观看| 亚洲乱码国产乱码精品精天堂| 日韩av在线免费播放| 日韩三级影视基地| 久久免费精品视频| 黑人狂躁日本妞一区二区三区| 狠狠色香婷婷久久亚洲精品| 久久99亚洲精品| 午夜精品理论片| 欧美激情乱人伦| 91午夜理伦私人影院| 91影视免费在线观看| 国产不卡av在线免费观看| 欧美激情国产日韩精品一区18| 欧美极品xxxx| 久久男人的天堂| 欧美成人国产va精品日本一级| 98午夜经典影视| 久久精品人人爽| 亚洲欧洲在线观看| 精品伊人久久97| 国产精品免费久久久久影院| 欧美午夜影院在线视频| 日韩禁在线播放| 欧美中文字幕精品| 91在线观看免费观看| 人体精品一二三区| 欧美大荫蒂xxx| 欧美性猛交xxxx乱大交极品| 成人国产精品色哟哟| 国产精品一区二区三区在线播放| 亚洲成成品网站| 两个人的视频www国产精品| 成人福利网站在线观看11| 日韩欧美国产一区二区| 欧美性猛交xxxx乱大交| 亚洲人成电影网站色…| 久久国产精品久久国产精品| 91在线免费视频| 91久久久久久久一区二区| 疯狂欧美牲乱大交777| 国产精品视频内| 欧美特黄级在线| 狠狠色狠色综合曰曰| 成人免费观看a| 欧美一级淫片丝袜脚交| 国产91久久婷婷一区二区| 国产成一区二区| 中文字幕免费精品一区| 国产精品99蜜臀久久不卡二区| 久久久久这里只有精品| 91精品久久久久久久久久久久久| 美女久久久久久久久久久| 日韩精品免费在线视频| 成人在线视频福利| 亚洲国产91精品在线观看| 91在线视频成人| 亚洲欧美中文日韩v在线观看| 欧美高清性猛交| 亚洲精品天天看| 亚洲人高潮女人毛茸茸| 日韩欧美视频一区二区三区| 国产成人a亚洲精品| 国产精品美女久久久免费| 日韩精品中文字| 久久国产精品久久久久久久久久| 日韩在线观看网址| 亚洲自拍偷拍网址| 一区二区三区视频免费在线观看| 一本一本久久a久久精品综合小说| 欧美一区在线直播| 精品国内产的精品视频在线观看| 欧美国产日韩免费| 欧美激情一级二级| 欧美电影免费看| 国产精品88a∨| 日本成人激情视频| 精品亚洲一区二区三区在线观看| 欧美极品少妇全裸体| 日韩在线播放视频| 视频在线观看一区二区| 欧美日本在线视频中文字字幕| 亚洲第一男人av| 国产亚洲激情视频在线| 国产香蕉精品视频一区二区三区| 久久97精品久久久久久久不卡| 国产精品久久久久久av福利| 亚洲第五色综合网| 欧美激情国产日韩精品一区18| 欧美日韩第一视频| 亚洲视频国产视频| 在线观看精品国产视频| 中文字幕无线精品亚洲乱码一区| 久精品免费视频| 亚洲电影免费观看高清| 久久久久久久久久婷婷| 美日韩精品免费观看视频| 国内精品久久久久久中文字幕| 亚洲国产高清自拍| 国语自产偷拍精品视频偷| 日韩精品久久久久| 九九精品在线视频| 国产一区二区三区在线观看网站| 日韩在线视频免费观看高清中文| 亚洲电影第1页| 91在线高清免费观看| 6080yy精品一区二区三区| 欧美日韩一区二区在线播放| 国产又爽又黄的激情精品视频| 亚洲毛片在线看| 美女扒开尿口让男人操亚洲视频网站| 色综合影院在线| 久久精品国产96久久久香蕉| 69久久夜色精品国产69| 亚洲久久久久久久久久久| 久久免费成人精品视频| 精品成人久久av| 伊人激情综合网| 国产精品狠色婷| 精品国产91久久久久久老师| 亚洲精品综合精品自拍| 国产一区二区三区在线视频| 国产不卡av在线免费观看| 亚洲最大成人网色| 久久精品视频在线观看| 国产亚洲精品va在线观看| 国产精品色视频| 国产91免费看片| 久久在线免费视频| 亚洲第一精品夜夜躁人人躁| 国产美女搞久久| 欧美在线精品免播放器视频| 奇米影视亚洲狠狠色| 久久久久久免费精品| 2018中文字幕一区二区三区| 欧美视频专区一二在线观看| 91精品国产网站| 日韩欧美在线一区| 欧美电影免费看| 麻豆国产精品va在线观看不卡| 精品国产精品三级精品av网址| 亚洲无限乱码一二三四麻| 欧美性猛交xxxx黑人猛交| 亚洲欧美国产日韩天堂区| 国产日韩精品在线播放| 一区二区三欧美| 在线国产精品视频| 亚洲欧美日韩精品久久| 欧美大片在线看免费观看| 日韩av在线免费| 国产极品jizzhd欧美| 中文字幕国产亚洲| 91精品国产色综合| 国产精品中文久久久久久久| 国产欧美精品在线播放| 国产精品一区二区三区久久| 国产精品福利在线观看| 日本成人在线视频网址| 日韩精品视频免费在线观看| 日韩性xxxx爱|