集合框架(四)
常用Collection之ArrayList
一、繼承關系
AbstractCollection<—AbstractList<—ArrayList二、原理
transient Object[] elementData; //elementData用來存放數據 底層是用array實現完成的三、構造方法
public ArrayList(int initialCapacity);//創建一個初始容量為initialCapacity的Object數組public ArrayList();//創建一個初始容量為10的Object數組(調用構造方法時,僅僅創建一個空數組,調用add方法時重新創建數組,數組長度為默認值10) public ArrayList(Collection<? extends E> c);//先將c轉為數組,并賦值給elementData,然后elementData數組類型向上轉型為Object四、常用方法實現原理
1、private void grow(int minCapacity);//改變數組長度,起增長方式為每次增加原數組長度的一半{int newCapacity = oldCapacity + (oldCapacity >> 1);}2、public Object[] toArray();//調用Arrays.copyOf()方法{return Arrays.copyOf(elementData, size);} 3、public boolean add(E e);//如果元素元素為空,該方法會使得容量最小為10{if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); }} 4、public void add(int index, E element);//{System.arraycopy(elementData, index, elementData, index + 1,size - index);}將源數組從index處開始復制到目標(自身)從index+1處開始,然后將index處的值重設為element 5、public boolean remove(Object o) ;//遍歷數組,找到o所在的index,刪除index,并調用fastRemove(int index)方法,將index右面的數據整體左移6、private void fastRemove(int index);//{System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--size] = null; }7、 public Iterator<E> iterator();//{return new Itr();}Itr為內部類五、迭代器實現
注:cursor沒有手動設初始值(int型默認值為0,創建對象時,便會進行初始化動作),我一眼看到也是一眼懵逼。 hasNext()方法和next()方法還有remove()方法實現原理還是相對簡單。 private class Itr implements Iterator<E> { int cursor; // index of next element to returnint lastRet = -1; // index of last element returned; -1 if no suchint 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();}}@Override@SuppressWarnings("unchecked")public void forEachRemaining(Consumer<? super E> consumer) {Objects.requireNonNull(consumer);final int size = ArrayList.this.size;int i = cursor; if (i >= size) {return;}final Object[] elementData = ArrayList.this.elementData;if (i >= elementData.length) {throw new ConcurrentModificationException();}while (i != size && modCount == expectedModCount) {consumer.accept((E) elementData[i++]);}// update once at end of iteration to reduce heap write trafficcursor = i;lastRet = i - 1;checkForComodification();}final void checkForComodification() {if (modCount != expectedModCount)throw new ConcurrentModificationException();} }六、總結
以array實現完成的List。允許快速訪問和隨機訪問,但當元素的安插或移除發生在List中央位置時,效率很差(從原數組到原數組復制,查看add(index,ele)方法可知)。可以選擇ArrayList遍歷走訪元素,如果有較多的插入和移除動作,最好選擇LinkedList(將在"集合框架(六)"中介紹)。轉載于:https://www.cnblogs.com/realsoul/p/5702224.html
總結
- 上一篇: Zabbix-3.0.3结合Grafan
- 下一篇: [20160725]MyComparab