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

首頁 > 編程 > Java > 正文

Java學習之ArrayList

2019-11-06 06:42:07
字體:
來源:轉載
供稿:網友

我們都站在巨人的肩膀上


1.ArrayList

源碼

public class ArrayList<E> extends AbstractList<E> implements List<E>, Randomaccess, Cloneable, java.io.Serializable{ PRivate static final long serialVersionUID = 8683452581122892189L; //默認容量 private static final int DEFAULT_CAPACITY = 10; //空集合 private static final Object[] EMPTY_ELEMENTDATA = {}; //數據集合 private transient Object[] elementData; private int size; //指定容量大小初始化 public ArrayList(int initialCapacity) { super(); if (initialCapacity < 0) throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); this.elementData = new Object[initialCapacity]; } public ArrayList() { super(); this.elementData = EMPTY_ELEMENTDATA; } //指定數據集合初始化 public ArrayList(Collection<? extends E> c) { elementData = c.toArray(); size = elementData.length; // c.toArray might (incorrectly) not return Object[] (see 6260652) if (elementData.getClass() != Object[].class) elementData = Arrays.copyOf(elementData, size, Object[].class); } //調整集合 public void trimToSize() { modCount++; if (size < elementData.length) { elementData = Arrays.copyOf(elementData, size); } } //指定集合的最小容量 public void ensureCapacity(int minCapacity) { int minExpand = (elementData != EMPTY_ELEMENTDATA) // any size if real element table ? 0 // larger than default for empty table. It's already supposed to be // at default size. : DEFAULT_CAPACITY; if (minCapacity > minExpand) { ensureExplicitCapacity(minCapacity); } } private void ensureCapacityInternal(int minCapacity) { if (elementData == EMPTY_ELEMENTDATA) { minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); } ensureExplicitCapacity(minCapacity); } private void ensureExplicitCapacity(int minCapacity) { modCount++; // overflow-conscious code if (minCapacity - elementData.length > 0) grow(minCapacity); } //指定VM最大值,超出發生異常 private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; //檢查容量是否足夠,然后擴容 private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; //擴容 int newCapacity = oldCapacity + (oldCapacity >> 1); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); // minCapacity is usually close to size, so this is a win: elementData = Arrays.copyOf(elementData, newCapacity); } private static int hugeCapacity(int minCapacity) { if (minCapacity < 0) // overflow throw new OutOfMemoryError(); return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE; } public int size() { return size; } public boolean isEmpty() { return size == 0; } public boolean contains(Object o) { return indexOf(o) >= 0; } public int indexOf(Object o) { if (o == null) { for (int i = 0; i < size; i++) if (elementData[i]==null) return i; } else { for (int i = 0; i < size; i++) if (o.equals(elementData[i])) return i; } return -1; } public int lastIndexOf(Object o) { if (o == null) { for (int i = size-1; i >= 0; i--) if (elementData[i]==null) return i; } else { for (int i = size-1; i >= 0; i--) if (o.equals(elementData[i])) return i; } return -1; } public Object clone() { try { @SuppressWarnings("unchecked") ArrayList<E> v = (ArrayList<E>) super.clone(); v.elementData = Arrays.copyOf(elementData, size); v.modCount = 0; return v; } catch (CloneNotSupportedException e) { // this shouldn't happen, since we are Cloneable throw new InternalError(); } } public Object[] toArray() { return Arrays.copyOf(elementData, size); } //講數組轉成集合 @SuppressWarnings("unchecked") public <T> T[] toArray(T[] a) { if (a.length < size) // Make a new array of a's runtime type, but my contents: return (T[]) Arrays.copyOf(elementData, size, a.getClass()); //public static void arraycopy(Object src, // int srcPos, // Object dest, // int destPos, // int length) //src:源數組; srcPos:源數組要復制的起始位置; //dest:目的數組; destPos:目的數組放置的起始位置; length:復制的長度。 System.arraycopy(elementData, 0, a, 0, size); if (a.length > size) a[size] = null; return a; } @SuppressWarnings("unchecked") E elementData(int index) { return (E) elementData[index]; } public E get(int index) { rangeCheck(index); return elementData(index); } public E set(int index, E element) { //檢查下標 rangeCheck(index); E oldValue = elementData(index); elementData[index] = element; return oldValue; } public boolean add(E e) { // 增量 ensureCapacityInternal(size + 1); elementData[size++] = e; return true; } public void add(int index, E element) { rangeCheckForAdd(index); ensureCapacityInternal(size + 1); // Increments modCount!! System.arraycopy(elementData, index, elementData, index + 1, size - index); elementData[index] = element; size++; } public E remove(int index) { rangeCheck(index); modCount++; E oldValue = elementData(index); int numMoved = size - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index, numMoved); // 末位設置為null,JVM GC機制回收 elementData[--size] = null; return oldValue; } public boolean remove(Object o) { if (o == null) { for (int index = 0; index < size; index++) if (elementData[index] == null) { fastRemove(index); return true; } } else { for (int index = 0; index < size; index++) if (o.equals(elementData[index])) { fastRemove(index); return true; } } return false; } private void fastRemove(int index) { modCount++; int numMoved = size - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index, numMoved); // 末位設置為null,JVM GC機制回收 elementData[--size] = null; } public void clear() { modCount++; // 所有設置為null,JVM GC機制回收 for (int i = 0; i < size; i++) elementData[i] = null; size = 0; } public boolean addAll(Collection<? extends E> c) { Object[] a = c.toArray(); int numNew = a.length; ensureCapacityInternal(size + numNew); // 增量 System.arraycopy(a, 0, elementData, size, numNew); size += numNew; return numNew != 0; } public boolean addAll(int index, Collection<? extends E> c) { rangeCheckForAdd(index); Object[] a = c.toArray(); int numNew = a.length; ensureCapacityInternal(size + numNew); // Increments modCount int numMoved = size - index; if (numMoved > 0) System.arraycopy(elementData, index, elementData, index + numNew, numMoved); System.arraycopy(a, 0, elementData, index, numNew); size += numNew; return numNew != 0; } protected void removeRange(int fromIndex, int toIndex) { modCount++; int numMoved = size - toIndex; System.arraycopy(elementData, toIndex, elementData, fromIndex, numMoved); // clear to let GC do its work int newSize = size - (toIndex-fromIndex); for (int i = newSize; i < size; i++) { elementData[i] = null; } size = newSize; } private void rangeCheck(int index) { if (index >= size) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } private void rangeCheckForAdd(int index) { if (index > size || index < 0) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } // 越界信息 private String outOfBoundsMsg(int index) { return "Index: "+index+", Size: "+size; } public boolean removeAll(Collection<?> c) { return batchRemove(c, false); } public boolean retainAll(Collection<?> c) { return batchRemove(c, true); } private boolean batchRemove(Collection<?> c, boolean complement) { final Object[] elementData = this.elementData; int r = 0, w = 0; boolean modified = false; try { for (; r < size; r++) if (c.contains(elementData[r]) == complement) elementData[w++] = elementData[r]; } finally { // 保存與收集的行為兼容性 if (r != size) { System.arraycopy(elementData, r, elementData, w, size - r); w += size - r; } if (w != size) { //多余的容量,GC回收 for (int i = w; i < size; i++) elementData[i] = null; modCount += size - w; size = w; modified = true; } } return modified; } // 寫入實例流 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; // 閱讀所有,包括隱藏的信息 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(); } } } // 迭代器,檢查下標 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(); } // 重寫 迭代器 private class Itr implements Iterator<E> { int cursor; // 下一個元素的索引 int lastRet = -1; // 返回最后一個元素的索引,如果沒有返回-1 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; expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); } } /** * 優化版的 迭代器 */ 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(); } } } 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 + ")"); } // 返回指定集合 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; } 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
国产精品久久99久久| www.xxxx精品| 亚洲视频自拍偷拍| 欧美亚洲成人精品| 91精品国产91久久久久| 亚洲免费电影一区| 亚洲男人第一av网站| 欧美电影在线观看完整版| 国产网站欧美日韩免费精品在线观看| 亚洲福利视频网| 久久亚洲国产精品成人av秋霞| 在线观看日韩专区| 91影院在线免费观看视频| 欧美性猛交丰臀xxxxx网站| 欧美夫妻性生活视频| 久久精品电影网站| 亚洲美女免费精品视频在线观看| 成人羞羞国产免费| 欧美激情乱人伦| 国产精品日韩专区| 国产精品第七影院| 国内偷自视频区视频综合| 精品久久久国产| 亚洲视频电影图片偷拍一区| 麻豆乱码国产一区二区三区| 992tv在线成人免费观看| 欧美激情18p| 国产日韩欧美视频| 成人免费直播live| 2019亚洲男人天堂| 国产精品久久久久久久9999| 色综合天天综合网国产成人网| 精品国产欧美一区二区三区成人| 亚洲欧美在线播放| 尤物tv国产一区| 欧美激情欧美狂野欧美精品| 国产一区深夜福利| 亚洲精品美女久久久久| 亚洲第一偷拍网| 国产欧美一区二区三区视频| 久久噜噜噜精品国产亚洲综合| 日韩电影中文字幕一区| 日韩一区二区av| 久久天天躁狠狠躁夜夜爽蜜月| 中文欧美在线视频| 久久久99久久精品女同性| 成人在线一区二区| 亚洲人成在线免费观看| 黑人巨大精品欧美一区免费视频| 国产精品国产三级国产aⅴ浪潮| 91成人免费观看网站| 91精品视频在线看| 不卡av电影在线观看| 国产有码在线一区二区视频| 精品视频在线导航| 中文日韩在线观看| 亚洲天堂免费观看| 国产高清视频一区三区| 日韩精品在线影院| 日韩一区二区三区在线播放| 精品久久久久久亚洲精品| 国产在线视频91| 狠狠躁夜夜躁人人爽超碰91| 亚洲欧洲成视频免费观看| 国产午夜精品免费一区二区三区| 清纯唯美亚洲激情| 国产精品久久久久久久天堂| 国产精品高潮在线| 国产精品扒开腿做爽爽爽视频| 欧美另类极品videosbest最新版本| xxx成人少妇69| 亚洲日本成人网| 日韩av手机在线| 欧美电影在线观看完整版| 国内精品400部情侣激情| 国产亚洲美女精品久久久| 亚洲伊人成综合成人网| 欧美一级视频在线观看| 国产精品美女av| 欧美人在线观看| 国产成人高潮免费观看精品| 91极品视频在线| 亚洲97在线观看| 国产精品爽爽爽| 欧美极品美女电影一区| 在线视频欧美性高潮| 久久久精品美女| 亚洲国产成人精品久久久国产成人一区| 亚洲人成电影在线播放| 国产精品久久一区主播| 亚洲人高潮女人毛茸茸| 狠狠久久五月精品中文字幕| 成人精品久久av网站| 久久久久久久久久久91| 亚洲精品国产免费| 亚洲成成品网站| 综合136福利视频在线| 国产欧美日韩综合精品| 欧美极品美女电影一区| 不卡av在线网站| 亚洲淫片在线视频| 欧美激情啊啊啊| 亚洲国产天堂网精品网站| 欧美精品一本久久男人的天堂| 国产精品综合不卡av| 亚洲自拍欧美另类| 欧美精品18videos性欧美| 欧美激情视频网站| 亚洲黄色有码视频| 中文字幕av一区| 久久久久久久999| 欧美日韩一区二区精品| 疯狂蹂躏欧美一区二区精品| 草民午夜欧美限制a级福利片| 国内精品久久久久伊人av| 26uuu另类亚洲欧美日本一| 97成人精品区在线播放| 欧美精品情趣视频| 91系列在线观看| 欧美亚洲另类制服自拍| 国产精品美女免费| 久久伊人精品天天| 奇米4444一区二区三区| 国产精品99久久久久久白浆小说| 午夜精品一区二区三区在线播放| 九九久久综合网站| 日韩av成人在线| 久久久久久18| 97久久精品国产| 欧美激情一级欧美精品| 日韩av在线最新| 成人网在线免费看| 精品久久中文字幕| 91免费国产视频| 人人爽久久涩噜噜噜网站| 日韩精品极品视频免费观看| 国产999精品久久久影片官网| 欧美午夜www高清视频| 久久99国产精品自在自在app| 亚洲女在线观看| 亚洲黄页视频免费观看| 亚洲午夜色婷婷在线| 欧美大片大片在线播放| 久久夜精品va视频免费观看| 亚洲欧美激情四射在线日| 国产精品老女人视频| 国产午夜精品美女视频明星a级| 久久精品99国产精品酒店日本| 日韩视频中文字幕| 这里只有精品在线播放| 久久九九国产精品怡红院| 国产成人在线亚洲欧美| 欧美最顶级的aⅴ艳星| 国产成人亚洲综合青青| 亚洲欧美在线免费观看| 98精品在线视频| 国产亚洲精品高潮| 亚洲一区二区三区视频播放| 红桃av永久久久| 91在线视频导航| 日韩精品在线观看一区二区| 亚洲欧美制服第一页| 欧美日韩国产黄| 亚洲香蕉成视频在线观看|