ArrayList源码
来源:互联网 发布:oracle sql创建表 编辑:程序博客网 时间:2024/05/21 22:33
一些参数:
private static final long serialVersionUID = 8683452581122892189L; // 序列号
/** * 初始的容量大小,构造函数没有指定大小,用此 */ private static final int DEFAULT_CAPACITY = 10;
/** * Shared empty array instance used for empty instances. * 空数组对象 */ private static final Object[] EMPTY_ELEMENTDATA = {};
/** * Shared empty array instance used for default sized empty instances. We * distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when * first element is added. * 默认的空数组对象,区别是当有第一元素被添加的时候,它就能知道增加了多大容量。就是指默认的10 */ private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
/** * The array buffer into which the elements of the ArrayList are stored. * The capacity of the ArrayList is the length of this array buffer. Any * empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA * will be expanded to DEFAULT_CAPACITY when the first element is added. * 数组缓存 */ transient Object[] elementData; // non-private to simplify nested class access
/** * The size of the ArrayList (the number of elements it contains). * ArrayList含有的元素个数 * @serial */ private int size;
构造方法:
/** * 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 */ public ArrayList(int initialCapacity) { if (initialCapacity > 0) { this.elementData = new Object[initialCapacity]; // 存入array buffer } else if (initialCapacity == 0) { this.elementData = EMPTY_ELEMENTDATA; // 空数组对象 } else { throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); // 非法参数 抛出异常 } }
/** * Constructs an empty list with an initial capacity of ten. * 无参构造 , 默认大小是 10 */ public ArrayList() { this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; }
/** * 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 */ public ArrayList(Collection<? extends E> c) { elementData = c.toArray(); if ((size = elementData.length) != 0) { // c.toArray might (incorrectly) not return Object[] (see 6260652) // c.toArray方法可能不会一直返回Object[]结果 如果不是我们需要把它转成同样的 if (elementData.getClass() != Object[].class) elementData = Arrays.copyOf(elementData, size, Object[].class); // 复制数组到数组缓存里 } else { // replace with empty array. this.elementData = EMPTY_ELEMENTDATA; // 空对象数组 } }
toArray:
/** * Returns an array containing all of the elements in this list * in proper sequence (from first to last element). */ public Object[] toArray() { return Arrays.copyOf(elementData, size);} public static <T> T[] copyOf(T[] original, int newLength) { return (T[]) copyOf(original, newLength, original.getClass());} public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) { @SuppressWarnings("unchecked") // 这句说明 返回的实际类型不一定是Object[] 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;}
扩容:
/** * Increases the capacity to ensure that it can hold at least the * number of elements specified by the minimum capacity argument. * * @param minCapacity the desired minimum capacity */private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; int newCapacity = oldCapacity + (oldCapacity >> 1); // 新容量大小是原来的1.5倍 if (newCapacity - minCapacity < 0) newCapacity = minCapacity; // 新容量小于ensureCapacity(int 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); // 新建数组}// 知道ArrayList的最大容量private static int hugeCapacity(int minCapacity) { if (minCapacity < 0) // overflow int 溢出成负数 throw new OutOfMemoryError(); return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE;}
常用方法:
// Positional Access Operations@SuppressWarnings("unchecked")E elementData(int index) { return (E) elementData[index];}
/** * Returns the element at the specified position in this list. * GET方法 * @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); // 判断是否过界,是抛出异常 return elementData(index); // 数组值 }
/** * Replaces the element at the specified position in this list with * the specified element. * * @param index index of the element to replace * @param element element to be stored at the specified position * @return the element previously at the specified position * @throws IndexOutOfBoundsException {@inheritDoc} */ public E set(int index, E element) { rangeCheck(index); // 判断是否过界,是抛出异常 E oldValue = elementData(index); elementData[index] = element; // 替换 return oldValue; // 返回原来的值 }
**末尾添加** /** * 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}) */ public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! (多线程的时候,fast-fail),并且观察是否需要扩容 elementData[size++] = e; // 添加 return true; // 添加成功 true }
**指定位置添加** /** * Inserts the specified element at the specified position in this * list. Shifts the element currently at that position (if any) and * any subsequent elements to the right (adds one to their indices). * * @param index index at which the specified element is to be inserted * @param element element to be inserted * @throws IndexOutOfBoundsException {@inheritDoc} */ public void add(int index, E element) { rangeCheckForAdd(index); ensureCapacityInternal(size + 1); // Increments modCount!! fast-fail System.arraycopy(elementData, index, elementData, index + 1, size - index); // 同一个数组重新复制从index+1 开始长度为 size-index elementData[index] = element; // 指定位置添加 size++; // ArrayList长度增加1 }
移除
/** * Removes the element at the specified position in this list. * Shifts any subsequent elements to the left (subtracts one from their * indices). * * @param index the index of the element to be removed * @return the element that was removed from the list * @throws IndexOutOfBoundsException {@inheritDoc} */ public E remove(int index) { rangeCheck(index); modCount++; // fast - fail 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 return oldValue; // 删除的值 }
/**不同与上一个方法 他不需要检查越界问题 * Removes the first occurrence of the specified element from this list, * if it is present. If the list does not contain the element, it is * unchanged. More formally, removes the element with the lowest index * <tt>i</tt> such that * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt> * (if such an element exists). Returns <tt>true</tt> if this list * contained the specified element (or equivalently, if this list * changed as a result of the call). * * @param o element to be removed from this list, if present * @return <tt>true</tt> if this list contained the specified element */ public boolean remove(Object o) { if (o == null) { for (int index = 0; index < size; index++) if (elementData[index] == null) { fastRemove(index); return true; } } else { for (int index = 0; index < size; index++) if (o.equals(elementData[index])) { fastRemove(index); return true; } } return false; // 删除成功 }
/* * Private remove method that skips bounds checking and does not * return the value removed. * 区别不需要 进行边界校验 */ private void fastRemove(int index) { modCount++; 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 }
/** * Removes all of the elements from this list. The list will * be empty after this call returns. * 清空ArrayList */ public void clear() { modCount++; // clear to let GC do its work for (int i = 0; i < size; i++) elementData[i] = null; size = 0; }
批量删除
public boolean removeAll(Collection<?> c) { Objects.requireNonNull(c); return batchRemove(c, false);}
批量保留
public boolean retainAll(Collection<?> c) { Objects.requireNonNull(c); return batchRemove(c, true);}
private boolean batchRemove(Collection<?> c, boolean complement) { final Object[] elementData = this.elementData; // final成内联效率高,安全等 int r = 0, w = 0; boolean modified = false; try { for (; r < size; r++) // complement = true 的时候保留, false 删除 if (c.contains(elementData[r]) == complement) elementData[w++] = elementData[r]; } finally { // Preserve behavioral compatibility with AbstractCollection, // even if c.contains() throws. 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; // 是否执行成功 }
遍历迭代器:
主要区别iterator可以向前遍历:listIterator public E previous()
阅读全文
0 0
- ArrayList源码
- ArrayList源码
- ArrayList源码
- ArrayList源码
- ArrayList源码
- ArrayList源码
- ArrayList源码
- ArrayList源码
- ArrayList源码
- 【源码】ArrayList源码剖析
- JDK源码-ArrayList源码
- ArrayList源码分析
- ArrayList源码分析
- ArrayList 源码分析
- ArrayList源码分析
- ArrayList LinkedList 源码分析
- java 源码ArrayList剖析
- ArrayList 源码解析
- 使用iOS手势UIGestureRecognizer(转)
- 【转载】iOS开发之屏幕旋转
- java学习第44天,hashCode
- UIView翻转效果实现(转)
- ios开发视图切换效果动画类CATransition
- ArrayList源码
- NSDictionary类使用(转)
- iOS ARC(转)
- ios开发-推送通知(转)
- iOS开发——keychain的使用(转)
- ios开发之app内起动用户评价 (转)
- 阐述提升游戏虚拟商品销售的5大技巧(转)
- 电脑硬盘速度明显变慢的原因
- How do I build Bitcoin source