public synchronized E get(int index) { if (index >= elementCount) throw new ArrayIndexOutOfBoundsException(index);
return elementData(index); }
E elementData(int index) { return (E) elementData[index]; }
synchronized修饰线程安全的,按照数组方式获取值,和ArrayList的方式相同
7 remove(int index)
public synchronized E remove(int index) { modCount++; if (index >= elementCount) throw new ArrayIndexOutOfBoundsException(index); E oldValue = elementData(index); int numMoved = elementCount - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--elementCount] = null; // Let gc do its work return oldValue; //有返回值,删除索引下元素的值 }
根据索引删除的主要方法调用的System.arraycopy()方法
7.1 arraycopy()方法
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);