日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

集合框架(四)

發布時間:2025/5/22 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 集合框架(四) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

常用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

總結

以上是生活随笔為你收集整理的集合框架(四)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。