/** * Constructs an empty list with an initial capacity of ten.(构造初始容量为10的空列表。) * 明明说是10,其实看代码只是创建了一个空数组,应该是版本更新忘了改注释了。 */ public ArrayList() { this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/** * 构造具有指定初始容量的空列表 */ public ArrayList(int initialCapacity) { if (initialCapacity > 0) { this.elementData = new Object[initialCapacity]; } else if (initialCapacity == 0) { this.elementData = EMPTY_ELEMENTDATA; } else { throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/** * 按照集合迭代器返回元素的顺序构造包含指定集合的元素的列表。 */ public ArrayList(Collection<? extends E> c) { elementData = c.toArray(); if ((size = elementData.length) != 0) { // c.toArray might (incorrectly) not return Object[] (see 6260652) if (elementData.getClass() != Object[].class) elementData = Arrays.copyOf(elementData, size, Object[].class); } else { // replace with empty array. this.elementData = EMPTY_ELEMENTDATA; } }
// 就是清空数组,也就是清空集合 public void clear() { modCount++;
// clear to let GC do its work for (int i = 0; i < size; i++) elementData[i] = null;
size = 0; }
clone
1 2 3 4 5 6 7 8 9 10 11 12 13
// 克隆集合。。 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 throw new InternalError(e); } }
contains
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
public boolean contains(Object o) { return indexOf(o) >= 0; } // // 从前面开始遍历,返回第一个与o相等的下标,不存在返回-1 public int indexOf(Object o) { if (o == null) { for (int i = 0; i < size; i++) if (elementData[i]==null) return i; } else { for (int i = 0; i < size; i++) if (o.equals(elementData[i])) return i; } return -1; }
get
1 2 3 4 5 6 7 8
// 根据下标值获取对应元素 public E get(int index) { // 检查是否超过最大下标值 rangeCheck(index); // 就是 elementData[index] return elementData(index); }
iterator
1 2 3 4
// 返回的是一个内部类 Itr,Itr这里就不做多解释了,有兴趣可以自己去看。 public Iterator<E> iterator() { return new Itr(); }
lastIndexOf
1 2 3 4 5 6 7 8 9 10 11 12 13
// 从后面开始遍历,返回第一个与o相等的下标,不存在返回-1 public int lastIndexOf(Object o) { if (o == null) { for (int i = size-1; i >= 0; i--) if (elementData[i]==null) return i; } else { for (int i = size-1; i >= 0; i--) if (o.equals(elementData[i])) return i; } return -1; }
remove
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 移除指定下标的元素,可以看到是用System.arraycopy来实现对移除元素后数组进行重新组合,让其他元素位置不变 public E remove(int index) { rangeCheck(index);
modCount++; E oldValue = elementData(index);
int numMoved = size - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--size] = null; // clear to let GC do its work
// 核心还是用到了System.arraycopy private boolean batchRemove(Collection<?> c, boolean complement) { final Object[] elementData = this.elementData; int r = 0, w = 0; boolean modified = false; try { for (; r < size; r++) // complement等于false,也就是c不包含的元素,重新从下标0赋值到elementData if (c.contains(elementData[r]) == complement) elementData[w++] = elementData[r]; } finally { if (r != size) { System.arraycopy(elementData, r, elementData, w, size - r); w += size - r; } if (w != size) { // clear to let GC do its work for (int i = w; i < size; i++) elementData[i] = null; modCount += size - w; size = w; modified = true; } } return modified; }
set
1 2 3 4 5 6 7 8
// 设值。。 public E set(int index, E element) { rangeCheck(index);
E oldValue = elementData(index); elementData[index] = element; return oldValue; }
sort
1 2 3 4 5 6 7 8 9
// 对elementData进行排序,用Arrays.sort实现 public void sort(Comparator<? super E> c) { final int expectedModCount = modCount; Arrays.sort((E[]) elementData, 0, size, c); if (modCount != expectedModCount) { throw new ConcurrentModificationException(); } modCount++; }
subList
1 2 3 4 5
// 截取指定访问的元素到SubList.,也就是生成新的集合吧 public List<E> subList(int fromIndex, int toIndex) { subListRangeCheck(fromIndex, toIndex, size); return new SubList(this, 0, fromIndex, toIndex); }
toArray
1 2 3 4
// 转换成size大小的数组。。。 public Object[] toArray() { return Arrays.copyOf(elementData, size); }
1 2 3 4 5 6 7 8 9 10
// 还可以指定转换的类型 public <T> T[] toArray(T[] a) { if (a.length < size) // Make a new array of a's runtime type, but my contents: return (T[]) Arrays.copyOf(elementData, size, a.getClass()); System.arraycopy(elementData, 0, a, 0, size); if (a.length > size) a[size] = null; return a; }