ArrayListVector 源码分析.docx

上传人:b****7 文档编号:9624648 上传时间:2023-02-05 格式:DOCX 页数:38 大小:23.03KB
下载 相关 举报
ArrayListVector 源码分析.docx_第1页
第1页 / 共38页
ArrayListVector 源码分析.docx_第2页
第2页 / 共38页
ArrayListVector 源码分析.docx_第3页
第3页 / 共38页
ArrayListVector 源码分析.docx_第4页
第4页 / 共38页
ArrayListVector 源码分析.docx_第5页
第5页 / 共38页
点击查看更多>>
下载资源
资源描述

ArrayListVector 源码分析.docx

《ArrayListVector 源码分析.docx》由会员分享,可在线阅读,更多相关《ArrayListVector 源码分析.docx(38页珍藏版)》请在冰豆网上搜索。

ArrayListVector 源码分析.docx

ArrayListVector源码分析

ArrayList源代码

packagejava.util;

publicclassArrayListextendsAbstractListimplementsList,RandomAccess,Cloneable,java.io.Serializable

{

privatestaticfinallongserialVersionUID=8683452581122892189L;

//重要成员

privatestaticfinalintDEFAULT_CAPACITY=10;//默认大小

privatestaticfinalObject[]EMPTY_ELEMENTDATA={};//(空数组)

privatetransientObject[]elementData;//(底层的对象数组)

privateintsize;//(有效元素的个数)

publicArrayList(intinitialCapacity){

super();

if(initialCapacity<0)

thrownewIllegalArgumentException("IllegalCapacity:

"+

initialCapacity);

this.elementData=newObject[initialCapacity];

}

publicArrayList(){

super();

this.elementData=EMPTY_ELEMENTDATA;//初始化一个空数组

}

publicArrayList(Collection

extendsE>c){

elementData=c.toArray();

size=elementData.length;

//c.toArraymight(incorrectly)notreturnObject[](see6260652)

if(elementData.getClass()!

=Object[].class)

elementData=Arrays.copyOf(elementData,size,Object[].class);

}

publicvoidtrimToSize(){

modCount++;

if(size

elementData=Arrays.copyOf(elementData,size);

}

}

publicvoidensureCapacity(intminCapacity){

intminExpand=(elementData!

=EMPTY_ELEMENTDATA)

//anysizeifrealelementtable

?

0

//largerthandefaultforemptytable.It'salreadysupposedtobe

//atdefaultsize.

:

DEFAULT_CAPACITY;

if(minCapacity>minExpand){

ensureExplicitCapacity(minCapacity);

}

}

privatevoidensureCapacityInternal(intminCapacity){

if(elementData==EMPTY_ELEMENTDATA)

{

minCapacity=Math.max(DEFAULT_CAPACITY,minCapacity);

//DEFAULT_CAPACITY=10

}

ensureExplicitCapacity(minCapacity);

}

privatevoidensureExplicitCapacity(intminCapacity){

modCount++;

//overflow-consciouscode

if(minCapacity-elementData.length>0)

grow(minCapacity);

}

privatestaticfinalintMAX_ARRAY_SIZE=Integer.MAX_VALUE-8;

privatevoidgrow(intminCapacity){//扩容

//overflow-consciouscode

intoldCapacity=elementData.length;

intnewCapacity=oldCapacity+(oldCapacity>>1);

if(newCapacity-minCapacity<0)

newCapacity=minCapacity;

if(newCapacity-MAX_ARRAY_SIZE>0)

newCapacity=hugeCapacity(minCapacity);

//minCapacityisusuallyclosetosize,sothisisawin:

elementData=Arrays.copyOf(elementData,newCapacity);

}

privatestaticinthugeCapacity(intminCapacity){

if(minCapacity<0)//overflow

thrownewOutOfMemoryError();

return(minCapacity>MAX_ARRAY_SIZE)?

Integer.MAX_VALUE:

MAX_ARRAY_SIZE;

}

publicintsize(){//获取实际元素个数

returnsize;

}

publicbooleanisEmpty(){

returnsize==0;

}

publicbooleancontains(Objecto){

returnindexOf(o)>=0;

}

publicintindexOf(Objecto){

if(o==null){

for(inti=0;i

if(elementData[i]==null)

returni;

}else{

for(inti=0;i

if(o.equals(elementData[i]))//调用equals()方法,比较地址

returni;

}

return-1;

}

publicintlastIndexOf(Objecto){

if(o==null){

for(inti=size-1;i>=0;i--)

if(elementData[i]==null)

returni;

}else{

for(inti=size-1;i>=0;i--)

if(o.equals(elementData[i]))

returni;

}

return-1;

}

publicObjectclone(){

try{

@SuppressWarnings("unchecked")

ArrayListv=(ArrayList)super.clone();

v.elementData=Arrays.copyOf(elementData,size);

v.modCount=0;

returnv;

}catch(CloneNotSupportedExceptione){

//thisshouldn'thappen,sinceweareCloneable

thrownewInternalError();

}

}

 

publicObject[]toArray(){

returnArrays.copyOf(elementData,size);

}

@SuppressWarnings("unchecked")

publicT[]toArray(T[]a){

if(a.length

//Makeanewarrayofa'sruntimetype,butmycontents:

return(T[])Arrays.copyOf(elementData,size,a.getClass());

System.arraycopy(elementData,0,a,0,size);

if(a.length>size)

a[size]=null;

returna;

}

@SuppressWarnings("unchecked")

EelementData(intindex){

return(E)elementData[index];

}

publicEget(intindex){

rangeCheck(index);

returnelementData(index);

}

publicEset(intindex,Eelement){

rangeCheck(index);

EoldValue=elementData(index);

elementData[index]=element;

returnoldValue;

}

publicbooleanadd(Ee)//增加元素

{

ensureCapacityInternal(size+1);//IncrementsmodCount!

!

elementData[size++]=e;

returntrue;

}

publicvoidadd(intindex,Eelement){

rangeCheckForAdd(index);

ensureCapacityInternal(size+1);//IncrementsmodCount!

!

System.arraycopy(elementData,index,elementData,index+1,

size-index);

elementData[index]=element;

size++;

}

publicEremove(intindex){

rangeCheck(index);

modCount++;

EoldValue=elementData(index);

intnumMoved=size-index-1;

if(numMoved>0)

System.arraycopy(elementData,index+1,elementData,index,

numMoved);

elementData[--size]=null;//cleartoletGCdoitswork

returnoldValue;

}

publicbooleanremove(Objecto){

if(o==null){

for(intindex=0;index

if(elementData[index]==null){

fastRemove(index);

returntrue;

}

}else{

for(intindex=0;index

if(o.equals(elementData[index])){

fastRemove(index);

returntrue;

}

}

returnfalse;

}

privatevoidfastRemove(intindex){

modCount++;

intnumMoved=size-index-1;

if(numMoved>0)

System.arraycopy(elementData,index+1,elementData,index,

numMoved);

elementData[--size]=null;//cleartoletGCdoitswork

}

publicvoidclear(){

modCount++;

//cleartoletGCdoitswork

for(inti=0;i

elementData[i]=null;

size=0;

}

publicbooleanaddAll(Collection

extendsE>c){

Object[]a=c.toArray();

intnumNew=a.length;

ensureCapacityInternal(size+numNew);//IncrementsmodCount

System.arraycopy(a,0,elementData,size,numNew);

size+=numNew;

returnnumNew!

=0;

}

publicbooleanaddAll(intindex,Collection

extendsE>c){

rangeCheckForAdd(index);

Object[]a=c.toArray();

intnumNew=a.length;

ensureCapacityInternal(size+numNew);//IncrementsmodCount

intnumMoved=size-index;

if(numMoved>0)

System.arraycopy(elementData,index,elementData,index+numNew,numMoved);

System.arraycopy(a,0,elementData,index,numNew);

size+=numNew;

returnnumNew!

=0;

}

protectedvoidremoveRange(intfromIndex,inttoIndex){

modCount++;

intnumMoved=size-toIndex;

System.arraycopy(elementData,toIndex,elementData,fromIndex,numMoved);

//cleartoletGCdoitswork

intnewSize=size-(toIndex-fromIndex);

for(inti=newSize;i

elementData[i]=null;

}

size=newSize;

}

privatevoidrangeCheck(intindex){

if(index>=size)//数组越界会抛异常

thrownewIndexOutOfBoundsException(outOfBoundsMsg(index));

}

privatevoidrangeCheckForAdd(intindex){

if(index>size||index<0)

thrownewIndexOutOfBoundsException(outOfBoundsMsg(index));

}

privateStringoutOfBoundsMsg(intindex){

return"Index:

"+index+",Size:

"+size;

}

publicbooleanremoveAll(Collection

>c){

returnbatchRemove(c,false);

}

publicbooleanretainAll(Collection

>c){

returnbatchRemove(c,true);

}

publicListIteratorlistIterator(intindex){

if(index<0||index>size)

thrownewIndexOutOfBoundsException("Index:

"+index);

returnnewListItr(index);

}

publicListIteratorlistIterator(){

returnnewListItr(0);

}

publicIteratoriterator(){

returnnewItr();

}

privateclassItrimplementsIterator{

intcursor;//indexofnextelementtoreturn

intlastRet=-1;//indexoflastelementreturned;-1ifnosuch

intexpectedModCount=modCount;

publicbooleanhasNext(){

returncursor!

=size;

}

@SuppressWarnings("unchecked")

publicEnext(){

checkForComodification();

inti=cursor;

if(i>=size)

thrownewNoSuchElementException();

Object[]elementData=ArrayList.this.elementData;

if(i>=elementData.length)

thrownewConcurrentModificationException();

cursor=i+1;

return(E)elementData[lastRet=i];

}

publicvoidremove(){

if(lastRet<0)

thrownewIllegalStateException();

checkForComodification();

try{

ArrayList.this.remove(lastRet);

cursor=lastRet;

lastRet=-1;

expectedModCount=modCount;

}catch(IndexOutOfBoundsExceptionex){

thrownewConcurrentModificationException();

}

}

finalvoidcheckForComodification(){

if(modCount!

=expectedModCount)

thrownewConcurrentModificationException();

}

}

privateclassListItrextendsItrimplementsListIterator{

ListItr(intindex){

super();

cursor=index;

}

publicbooleanhasPrevious(){

returncursor!

=0;

}

publicintnextIndex(){

returncursor;

}

publicintpreviousIndex(){

returncursor-1;

}

@SuppressWarnings("unchecked")

publicEprevious(){

checkForComodification();

inti=cursor-1;

if(i<0)

thrownewNoSuchElementException();

Object[]elementData=ArrayList.this.elementData;

if(i>=elementData.length)

thrownewConcurrentModificationException();

cursor=i;

return(E)elementData[lastRet=i];

}

publicvoidset(Ee){

if(lastRet<0)

thrownewIllegalStateException();

checkForComodification();

try{

ArrayList.this.set(lastRet,e);

}catch(IndexOutOfBoundsExceptionex){

thrownewConcurrentModificationException();

}

}

publicvoidadd(Ee){

checkForComodification();

try{

inti=cursor;

ArrayList.this.add(i,e);

cursor=i+1;

lastRet=-1;

expectedModCount=modCount;

}catch(IndexOutOfBoundsExceptionex){

thrownewConcurrentModificationException();

}

}

}

publicListsubList(intfromIndex,inttoIndex){

subLi

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1