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

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

ArrayList源碼解析(下)

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

序列化操作,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
亚洲国产精品中文| 欧美精品生活片| 成人午夜黄色影院| 亚洲欧美激情精品一区二区| 一本大道香蕉久在线播放29| 大胆欧美人体视频| 国产精品极品美女粉嫩高清在线| 日韩欧美一区二区在线| 欧美最近摘花xxxx摘花| 97香蕉超级碰碰久久免费软件| 亚洲xxx大片| 中文字幕在线国产精品| 欧美体内谢she精2性欧美| 亚洲色图欧美制服丝袜另类第一页| 国产精品久久久久久久7电影| 精品高清一区二区三区| 91精品久久久久久久久不口人| 久久全国免费视频| 久久久精品国产| 97婷婷涩涩精品一区| 亚洲国产成人精品电影| 亚洲成人教育av| 91在线观看免费高清| 国产精品成人观看视频国产奇米| 欧美电影免费观看网站| 久久精品国产亚洲一区二区| 九九热精品视频| 国产精品第一页在线| 免费成人高清视频| 欧美午夜精品久久久久久浪潮| 成人午夜在线影院| 欧美多人爱爱视频网站| 国产精品三级久久久久久电影| 亚洲精品免费在线视频| 深夜成人在线观看| 亚洲国产精品久久久久秋霞不卡| 亚洲福利影片在线| 国产噜噜噜噜噜久久久久久久久| 欧美精品video| 亚洲图片欧美午夜| 久久久天堂国产精品女人| 久久天天躁狠狠躁夜夜av| 久久91超碰青草是什么| 欧美人与性动交a欧美精品| 日韩电影视频免费| 国产有码一区二区| 亚洲老头同性xxxxx| 中文国产亚洲喷潮| 2019中文字幕在线观看| 久久精品国产欧美激情| 国产精品吊钟奶在线| 国产69精品久久久| 91精品国产91久久久久久不卡| 色综合视频一区中文字幕| 97色在线视频| 欧美多人乱p欧美4p久久| 日韩欧美国产激情| 亚洲第一页自拍| 最近中文字幕日韩精品| 日韩女在线观看| 45www国产精品网站| 精品国产欧美一区二区三区成人| 欧美日本精品在线| 日韩欧美在线第一页| 6080yy精品一区二区三区| 国产99久久精品一区二区| 国产69精品久久久久9999| 美日韩丰满少妇在线观看| 久久91亚洲精品中文字幕奶水| 精品成人乱色一区二区| 欧美有码在线观看视频| 91夜夜未满十八勿入爽爽影院| 欧美影院久久久| 国产精品色视频| 在线看欧美日韩| 国模极品一区二区三区| 最近2019年日本中文免费字幕| 色综合久久88色综合天天看泰| 欧美午夜宅男影院在线观看| 欧美在线xxx| 日韩在线视频免费观看高清中文| 久久精品视频在线观看| 日韩欧美精品网址| 亚洲成人激情在线| 美日韩精品免费视频| 高跟丝袜欧美一区| 欧美刺激性大交免费视频| 亚洲人成77777在线观看网| 日韩av123| 亚洲www视频| 成人免费看片视频| 久久精品成人动漫| 欧美成人激情在线| 欧美午夜精品久久久久久人妖| 在线播放国产一区二区三区| 久久精品亚洲精品| 国产精品久久一区| 亚洲乱码一区av黑人高潮| 超碰精品一区二区三区乱码| 欧美高清一级大片| 国模精品视频一区二区三区| 国产成人久久久| 亚洲欧洲偷拍精品| 亚洲高清一二三区| 孩xxxx性bbbb欧美| 日韩欧美中文免费| 成人信息集中地欧美| 欧美极品少妇全裸体| 国产mv免费观看入口亚洲| 精品自在线视频| 91精品国产777在线观看| 在线看欧美日韩| 国产精品高清免费在线观看| 亚洲一区二区三区乱码aⅴ蜜桃女| 亚洲最大成人在线| 国产精品欧美一区二区| 中文字幕免费精品一区| 美女国内精品自产拍在线播放| 久久人人爽人人爽人人片av高清| 色偷偷av一区二区三区乱| 一区二区欧美日韩视频| 久久精品国产成人精品| 国产精品亚洲自拍| 欧美日韩国产中文精品字幕自在自线| 国产在线精品播放| 久久久久久国产三级电影| 久久天天躁夜夜躁狠狠躁2022| 欧美超级乱淫片喷水| 国产成人+综合亚洲+天堂| 欧美一区二区三区免费观看| 亚洲国产精品人久久电影| 91在线无精精品一区二区| 中文字幕久久精品| 亚洲黄一区二区| 国产成人自拍视频在线观看| 色综合影院在线| 日韩中文字幕视频在线观看| 欧美成年人视频网站| 亚洲精品电影网站| 国产精彩精品视频| 国产美女精品视频免费观看| 国产91精品高潮白浆喷水| 亚洲r级在线观看| 国产精品美女www爽爽爽视频| 亚洲国产成人在线视频| 色综合伊人色综合网站| 亚洲欧美国产日韩中文字幕| 亚洲视频在线免费观看| 成人激情在线播放| 久久精品国产清自在天天线| 亚洲成av人片在线观看香蕉| 亚洲欧美在线磁力| 欧美激情视频一区二区三区不卡| 午夜精品美女自拍福到在线| 日韩精品免费电影| 91精品国产自产在线| 在线精品视频视频中文字幕| 久久色在线播放| 浅井舞香一区二区| 成人国产精品免费视频| 日韩理论片久久| 国产精品美女www爽爽爽视频| 欧美黑人极品猛少妇色xxxxx| 国产欧美日韩亚洲精品|