【17期】什么情況用ArrayList or LinkedList呢?
閱讀本文大概需要 7 分鐘。
來自:網(wǎng)絡(luò)
列表(list)是元素的有序集合,也稱為序列。它提供了基于元素位置的操作,有助于快速訪問、添加和刪除列表中特定索引位置的元素。List 接口實現(xiàn)了 Collection 和 Iterable 作為父接口。它允許存儲重復值和空值,支持通過索引訪問元素。
增加元素到列表尾端:
public?boolean?add(E?e){
???ensureCapacity(size+1);//確保內(nèi)部數(shù)組有足夠的空間
???elementData[size++]=e;//將元素加入到數(shù)組的末尾,完成添加
???return?true;??????
}?
public?vod?ensureCapacity(int?minCapacity){
??modCount++;
??int?oldCapacity=elementData.length;
??if(minCapacity>oldCapacity){????//如果數(shù)組容量不足,進行擴容
??????Object[]?oldData=elementData;
??????int?newCapacity=(oldCapacity*3)/2+1;??//擴容到原始容量的1.5倍
??????if(newCapacitty//如果新容量小于最小需要的容量,則使用最小
????????????????????????????????????????????????????//需要的容量大小
?????????newCapacity=minCapacity?;??//進行擴容的數(shù)組復制
?????????elementData=Arrays.copyof(elementData,newCapacity);
??}
}
public?boolean?add(E?e){
???addBefore(e,header);//將元素增加到header的前面
???return?true;
}
private?Entry ?addBefore(E?e,Entry {?entry )
?????Entry?newEntry?=?new?Entry (e,entry,entry.previous);
?????newEntry.provious.next=newEntry;
?????newEntry.next.previous=newEntry;
?????size++;
?????modCount++;
?????return?newEntry;
}
增加元素到列表任意位置
void add(int index,E element);public?void?add(int?index,E?element){
???if(index>size||index<0)
??????throw?new?IndexOutOfBoundsException(
????????"Index:"+index+",size:?"+size);
?????????ensureCapacity(size+1);
?????????System.arraycopy(elementData,index,elementData,index+1,size-index);
?????????elementData[index]?=?element;
?????????size++;
}
public?void?add(int?index,E?element){
???addBefore(element,(index==size?header:entry(index)));
}
刪除任意位置元素
public?E?remove(int?index);
public?E?remove(int?index){
???RangeCheck(index);
???modCount++;
???E?oldValue=(E)?elementData[index];
??int?numMoved=size-index-1;
??if(numMoved>0)
?????System.arraycopy(elementData,index+1,elementData,index,numMoved);
?????elementData[--size]=null;
?????return?oldValue;
}
public?E?remove(int?index){
??return?remove(entry(index));?????????
}
private?Entry?entry(int?index){
??if(index<0?||?index>=size)
??????throw?new?IndexOutBoundsException("Index:"+index+",size:"+size);
??????Entry?e=?header;
??????if(index<(size>>1)){//要刪除的元素位于前半段
?????????for(int?i=0;i<=index;i++)
?????????????e=e.next;
?????}else{
?????????for(int?i=size;i>index;i--)
?????????????e=e.previous;
?????}
?????????return?e;
}
容量參數(shù)
public??ArrayList(){
??this(10);??
}
public?ArrayList?(int?initialCapacity){
???super();
???if(initialCapacity<0)
???????throw?new?IllegalArgumentException("Illegal?Capacity:"+initialCapacity)
??????this.elementData=new?Object[initialCapacity];
}
public?ArrayList(int?initialCapacity)?
遍歷列表
forEach操作
迭代器
for循環(huán)。
String?tmp;
long?start=System.currentTimeMills();????//ForEach?
for(String?s:list){
????tmp=s;
}
System.out.println("foreach?spend:"+(System.currentTimeMills()-start));
start?=?System.currentTimeMills();
for(Iterator?it=list.iterator();it.hasNext();){????
???tmp=it.next();
}
System.out.println("Iterator?spend;"+(System.currentTimeMills()-start));
start=System.currentTimeMills();
int?size=;list.size();
for(int?i=0;i????tmp=list.get(i);
}
System.out.println("for?spend;"+(System.currentTimeMills()-start));

總結(jié)
1.對ArrayList和LinkedList而言,在列表末尾增加一個元素所花的開銷都是固定的。 對ArrayList而言,主要是在內(nèi)部數(shù)組中增加一項,指向所添加的元素,偶爾可能會導致對數(shù)組重新進行分配; 而對LinkedList而言,這個開銷是統(tǒng)一的,分配一個內(nèi)部Entry對象。
2.在ArrayList的中間插入或刪除一個元素意味著這個列表中剩余的元素都會被移動;而在LinkedList的中間插入或刪除一個元素的開銷是固定的。
3.LinkedList不支持高效的隨機元素訪問。
4.ArrayList的空間浪費主要體現(xiàn)在在list列表的結(jié)尾預(yù)留一定的容量空間,而LinkedList的空間花費則體現(xiàn)在它的每一個元素都需要消耗相當?shù)目臻g
推薦閱讀:
厲害了!手擼一個SpringBoot緩存系統(tǒng),性能杠杠的!
微信掃描二維碼,關(guān)注我的公眾號
朕已閱?
評論
圖片
表情

