//获取指定索引位置的元素publicsynchronized E get(int index){//如果索引位置大于等于数组元素个数,则抛出异常if(index >= elementCount)thrownewArrayIndexOutOfBoundsException(index);returnelementData(index);}
E elementData(int index){//直接从数组下标获取元素return(E) elementData[index];}
set()方法
//将指定索引位置的元素替换成传入元素publicsynchronized E set(int index, E element){//索引检查if(index >= elementCount)thrownewArrayIndexOutOfBoundsException(index);//获取旧值
E oldValue =elementData(index);//赋新值
elementData[index]= element;//返回旧值return oldValue;}
remove()方法
//删除指定位置的元素publicsynchronized E remove(int index){
modCount++;//索引检查if(index >= elementCount)thrownewArrayIndexOutOfBoundsException(index);//获取旧值
E oldValue =elementData(index);//计算将要移动的元素个数int numMoved = elementCount - index -1;//如果大于0,则移动if(numMoved >0)//元素移动
System.arraycopy(elementData, index+1, elementData, index,
numMoved);//让最后位置元素赋null
elementData[--elementCount]= null;// Let gc do its work//返回旧值return oldValue;}
//删除指定元素publicbooleanremove(Object o){returnremoveElement(o);}publicsynchronizedbooleanremoveElement(Object obj){
modCount++;//获取当前元素的索引int i =indexOf(obj);//索引大于0,则调用removeElementAt(i);删除if(i >=0){removeElementAt(i);returntrue;}returnfalse;}publicsynchronizedvoidremoveElementAt(int index){
modCount++;//检查索引if(index >= elementCount){thrownewArrayIndexOutOfBoundsException(index +" >= "+
elementCount);}//检查索引elseif(index <0){thrownewArrayIndexOutOfBoundsException(index);}//计算将要移动的元素个数int j = elementCount - index -1;//如果大于0,则移动if(j >0){//元素移动
System.arraycopy(elementData, index +1, elementData, index, j);}
elementCount--;//让最后位置元素赋null
elementData[elementCount]= null;/* to let gc do its work */}