/** * Returns a shallow copy of this <tt>ArrayList</tt> instance. (The * elements themselves are not copied.) * * @return a clone of this <tt>ArrayList</tt> instance */ public Object clone() { try { ArrayList<?> v = (ArrayList<?>) super.clone(); v.elementData = Arrays.copyOf(elementData, size); v.modCount = 0; return v; } catch (CloneNotSupportedException e) { // this shouldn't happen, since we are Cloneable thrownewInternalError(e); } }
/** * Constructs an empty list with an initial capacity of ten. */ //构造一个初始容量为10的数组 //DEFAULTCAPACITY_EMPTY_ELEMENTDATA:默认的空容量的数组 //elementData:集合真正存储数据的容器 publicArrayList() { this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; }
ArrayList(int initialCapacity) 构造具有指定初始容量的空列表
/** * Constructs an empty list with the specified initial capacity. * * @param initialCapacity the initial capacity of the list * @throws IllegalArgumentException if the specified initial capacity * is negative */ publicArrayList(int initialCapacity) { if (initialCapacity > 0) { //如果传进来的变量大于0,则初始化一个指定容量的空数组 this.elementData = newObject[initialCapacity]; } elseif (initialCapacity == 0) { //传进来的变量=0,则不去创建新的数组,直接将已创建的EMPTY_ELEMENTDATA空数组传给ArrayList this.elementData = EMPTY_ELEMENTDATA; } else { //传进来的指定数组容量不能<0 thrownewIllegalArgumentException("Illegal Capacity: "+ initialCapacity); }
ArrayList(Collection<? extends E> c) 构造一个包含指定集合的元素的列表,按照它们由集合的迭代器返回的顺序
/** * Constructs a list containing the elements of the specified * collection, in the order they are returned by the collection's * iterator. * * @param c the collection whose elements are to be placed into this list * @throws NullPointerException if the specified collection is null */ publicArrayList(Collection<? extends E> c) { //将构造方法中的参数转换成数组形式,其底层是调用了System.arraycopy() elementData = c.toArray(); //将数组的长度赋值给size if ((size = elementData.length) != 0) { //检查elementData是不是Object[]类型,不是的话将其转换成Object[].class类型 if (elementData.getClass() != Object[].class) //数组的创建与拷贝 elementData = Arrays.copyOf(elementData, size, Object[].class); } else { //size为0,则把已创建好的空数组直接给它 this.elementData = EMPTY_ELEMENTDATA; } }
/** * Appends the specified element to the end of this list. * * @param e element to be appended to this list * @return <tt>true</tt> (as specified by {@link Collection#add}) */ //将指定的元素追加到此列表的末尾 publicbooleanadd(E e) { //每增加1个元素,数组所需容量+1,并检查增加数组容量后是否要扩容 ensureCapacityInternal(size + 1); // Increments modCount!! //添加元素 elementData[size++] = e; returntrue; }
public E set(int index, E element) { //判断索引是否符合规则,索引不能超过数组长度 rangeCheck(index); //获得下标处的元素 EoldValue= elementData(index); //修改索引处的元素值 elementData[index] = element; //将旧的元素值返回 return oldValue; }
//校验索引 privatevoidrangeCheck(int index) { if (index >= size) thrownewIndexOutOfBoundsException(outOfBoundsMsg(index)); } //获得下标处的元素 E elementData(int index) { return (E) elementData[index]; }
get(int index)
获得索引处的元素值
/** * Returns the element at the specified position in this list. * * @param index index of the element to return * @return the element at the specified position in this list * @throws IndexOutOfBoundsException {@inheritDoc} */ public E get(int index) { rangeCheck(index);