接着说完ArrayList“改查”算法。
ArrayList
1.改
public E set(int index, E element)
public void trimToSize()
ArrayList中关于“改”的方法应该只有上面两个了,我们先去研究下set方法,源码奉上先:
public E set(int index, E element) {
rangeCheck(index);
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}
先是检测下传入的index值是否超出了ArrayList的长度范围,然后,简单,看下一个方法
public void trimToSize() {
modCount++;
int oldCapacity = elementData.length;
if (size < oldCapacity) {
elementData = Arrays.copyOf(elementData, size);
}
}
重点在Arrays的静态方法copyOf,功能应该是从传入的数组中复制一段从0至size长度的小数组,并返回这个小数组。看下copyOf的源码
public static <T> T[] copyOf(T[] original, int newLength) {
return (T[]) copyOf(original, newLength, original.getClass());
}
看下三个参数的copyOf方法
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength);
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
原来还是调用了System的arraycopy方法呀。
2.查
public boolean contains(Object o)
public E get(int index)
public int indexOf(Object o)
public boolean isEmpty()
public int lastIndexOf(Object o)
isEmpty方法应该是最简单的,判断下size的值是否等于0就可以了。get方法的算法也很简单,首先检测下传入的index是否超出范围,然后就是根据索引值返回数组中对应的值了。contains方法,核心应该在indexOf里面,看contains方法在源码。
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
很好,看重点
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;
}
返回此列表中首次出现的指定元素的索引,或如果此列表不包含元素,则返回 -1。我们这里注意下当对象为空时,若列表中也存在一个为空的对象时,也会返回对应的索引的。lastIndexOf实现算法和indexOf有异曲同工之妙,在此就不再赘述了。
3.其它
public boolean retainAll(Collection<?> c)
public List<E> subList(int fromIndex, int toIndex)
public Object clone()
retainAll这是一个求与指定集合交集的方法,很实用的一个方法,内部现实调用了batchRemove,上面我有说过这个方法,所以看下一个方法subList,取得ArrayList的部分段方法,源码
public List<E> subList(int fromIndex, int toIndex) {
subListRangeCheck(fromIndex, toIndex, size);
return new SubList(this, 0, fromIndex, toIndex);
}
可以看下subListRangeCheck方法,应该比较有意思
static void subListRangeCheck(int fromIndex, int toIndex, int size) {
if (fromIndex < 0)
throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
if (toIndex > size)
throw new IndexOutOfBoundsException("toIndex = " + toIndex);
if (fromIndex > toIndex)
throw new IllegalArgumentException("fromIndex(" + fromIndex +
") > toIndex(" + toIndex + ")");
}
今天就到这里了。