//这个扩容方法,内部没有调用,判断ArrayList是否为空 publicvoidensureCapacity(int minCapacity) { intminExpand= (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA) // any size if not default element table ? 0 // larger than default for default empty table. It's already // supposed to be at default size. : DEFAULT_CAPACITY;
if (minCapacity > minExpand) { ensureExplicitCapacity(minCapacity); } }
privatevoidgrow(int minCapacity) { // 计算新数组长度 intoldCapacity= elementData.length; //new = old * 1.5 默认数组扩容量 intnewCapacity= oldCapacity + (oldCapacity >> 1); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); // minCapacity is usually close to size, so this is a win: //将原数组数据复制入新数组中 elementData = Arrays.copyOf(elementData, newCapacity); }
//顺序遍历查找元素 publicintindexOf(Object o) { if (o == null) {//如果o为null,则在数组中寻找null for (inti=0; i < size; i++)//注意循环至size,而不是elementData.length if (elementData[i]==null) return i; } else { for (inti=0; i < size; i++) if (o.equals(elementData[i])) return i;//返回下标 } return -1;//未找到匹配项返回-1 }
//倒序遍历查找元素,从此方法也可看出元素可重复 publicintlastIndexOf(Object o) { if (o == null) { for (inti= size-1; i >= 0; i--) if (elementData[i]==null) return i; } else { for (inti= size-1; i >= 0; i--) if (o.equals(elementData[i])) return i; } return -1; }
(4) toArray()
1 2 3 4 5 6 7 8 9 10 11 12 13 14
public Object[] toArray() { return Arrays.copyOf(elementData, size); }
public <T> T[] toArray(T[] a) { if (a.length < size) //若传入的数组长度小于size(ArrayList实际长度),则返回一个长度为size新的数组, 且将进行类型转换 return (T[]) Arrays.copyOf(elementData, size, a.getClass()); //若传入数组长度大于或等于elementData.length,则把elementData复制进传入数组 System.arraycopy(elementData, 0, a, 0, size); if (a.length > size)//若传入的a的长度大于原本数组,则最大下标后面补null a[size] = null; return a; }
(5) get,set
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
E elementData(int index) { return (E) elementData[index]; }
public E get(int index) {
rangeCheck(index);//index <= size
return elementData(index); }
//直接替换index位置元素,返回原下标内的值 public E set(int index, E element) { rangeCheck(index);// index <= size
publicbooleanremove(Object o) { if (o == null) { for (intindex=0; index < size; index++)//遍历至size if (elementData[index] == null) { fastRemove(index);//顺序遍历去除首个null returntrue; } } else { for (intindex=0; index < size; index++) if (o.equals(elementData[index])) { fastRemove(index); returntrue; } } returnfalse; }
publicvoidclear() { modCount++;
// clear to let GC do its work for (inti=0; i < size; i++) elementData[i] = null;
size = 0; }
看到这其实又产生了新的疑问,测试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
List<String> list = newArrayList<String>(); list.add("1"); list.add("2"); list.add("3"); list.add("3"); list.add("4"); list.add("5"); System.out.println("原来的list:" + list); for (String s : list) { if ("3".equals(s)) { list.remove(s); } } System.out.println("修改后的list:: " + list);
@SuppressWarnings("unchecked") public E next() { checkForComodification();//检查是否在创建了itr对象后list结构是否有改变 inti= cursor; if (i >= size) thrownewNoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) thrownewConcurrentModificationException(); cursor = i + 1;//索引+1 return (E) elementData[lastRet = i]; }
publicvoidremove() { if (lastRet < 0)//此判断表明调用remove前必须先调用next thrownewIllegalStateException(); checkForComodification();
//JDK1.8新添加方法,日后研究 @Override @SuppressWarnings("unchecked") publicvoidforEachRemaining(Consumer<? super E> consumer) { Objects.requireNonNull(consumer); finalintsize= ArrayList.this.size; inti= cursor; if (i >= size) { return; } final Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) { thrownewConcurrentModificationException(); } while (i != size && modCount == expectedModCount) { consumer.accept((E) elementData[i++]); } // update once at end of iteration to reduce heap write traffic cursor = i; lastRet = i - 1; checkForComodification(); }
finalvoidcheckForComodification() { if (modCount != expectedModCount) thrownewConcurrentModificationException(); } }