java ArrayList 的实现原理
2016-08-31 15:55
225 查看
1.ArrayList这是我们经常使用到的类,但是对于他是怎么实现的?我们来看一下:
数组的拷贝。
这里有一个值得注意的地方就是新数组的大小,怎么进行扩大呢?
/** * Constructs a new instance of {@code ArrayList} with the specified * initial capacity. * * @param capacity * the initial capacity of this {@code ArrayList}. */ public ArrayList(int capacity) { if (capacity < 0) { throw new IllegalArgumentException("capacity < 0: " + capacity); } array = (capacity == 0 ? EmptyArray.OBJECT : new Object[capacity]); } /** * Constructs a new {@code ArrayList} instance with zero initial capacity. */ public ArrayList() { array = EmptyArray.OBJECT; } /** * Constructs a new instance of {@code ArrayList} containing the elements of * the specified collection. * * @param collection * the collection of elements to add. */ public ArrayList(Collection<? extends E> collection) { if (collection == null) { throw new NullPointerException("collection == null"); } Object[] a = collection.toArray(); if (a.getClass() != Object[].class) { Object[] newArray = new Object[a.length]; System.arraycopy(a, 0, newArray, 0, a.length); a = newArray; } array = a; size = a.length; }这就是他的构造参数,从里面你可以看出,他就是一个数组,而他的重要属性就是这些:
/** * The minimum amount by which the capacity of an ArrayList will increase. * This tuning parameter controls a time-space tradeoff. This value (12) * gives empirically good results and is arguably consistent with the * RI's specified default initial capacity of 10: instead of 10, we start * with 0 (sans allocation) and jump to 12. */ private static final int MIN_CAPACITY_INCREMENT = 12; /** * The number of elements in this list. */ int size; /** * The elements in this list, followed by nulls. */ transient Object[] array;一个数组,一个大小,一个限定值。既然是一个数组那就我们就可以想象的到他的增删数据都是数组数据的移动,我们来验证一下我们的想法:
/** * Adds the specified object at the end of this {@code ArrayList}. * * @param object * the object to add. * @return always true */ @Override public boolean add(E object) { Object[] a = array; int s = size; if (s == a.length) { Object[] newArray = new Object[s + (s < (MIN_CAPACITY_INCREMENT / 2) ? MIN_CAPACITY_INCREMENT : s >> 1)]; System.arraycopy(a, 0, newArray, 0, s); array = a = newArray; } a[s] = object; size = s + 1; modCount++; return true; }果然,新建了一个数组,然后数据拷贝,然后新数据的赋值,在这个类中频繁的使用到了 System.arraycopy,Arrays.fill
数组的拷贝。
/** * Removes the object at the specified location from this list. * * @param index * the index of the object to remove. * @return the removed object. * @throws IndexOutOfBoundsException * when {@code location < 0 || location >= size()} */ @Override public E remove(int index) { Object[] a = array; int s = size; if (index >= s) { throwIndexOutOfBoundsException(index, s); } @SuppressWarnings("unchecked") E result = (E) a[index]; System.arraycopy(a, index + 1, a, index, --s - index); a[s] = null; // Prevent memory leak size = s; modCount++; return result; }
这里有一个值得注意的地方就是新数组的大小,怎么进行扩大呢?
/** * This method controls the growth of ArrayList capacities. It represents * a time-space tradeoff: we don't want to grow lists too frequently * (which wastes time and fragments storage), but we don't want to waste * too much space in unused excess capacity. * * NOTE: This method is inlined into {@link #add(Object)} for performance. * If you change the method, change it there too! */ private static int newCapacity(int currentCapacity) { int increment = (currentCapacity < (MIN_CAPACITY_INCREMENT / 2) ? MIN_CAPACITY_INCREMENT : currentCapacity >> 1); return currentCapacity + increment; }到这里,可以了解到ArrayList 实际就是一个数组,他不能保证线程安全。
相关文章推荐
- 深入Java集合学习系列:ArrayList的实现原理
- 深入Java集合学习系列:ArrayList的实现原理
- 深入Java集合学习系列:ArrayList的实现原理
- 深入Java集合学习系列:ArrayList的实现原理
- 深入Java集合学习系列:ArrayList的实现原理
- 深入Java集合学习系列:ArrayList的实现原理
- Java集合---ArrayList的实现原理
- java集合之ArrayList的实现原理
- Java初学习 - ArrayList的实现原理 及ArrayList的使用
- 深入Java集合学习系列:ArrayList的实现原理
- JAVA ArrayList的实现原理 及ArrayList的使用
- 深入Java集合学习系列:ArrayList的实现原理
- 深入java集合学习2-ArrayList的实现原理
- 深入Java集合学习系列:ArrayList的实现原理
- 深入Java集合学习系列:ArrayList的实现原理
- 深入Java集合学习系列:ArrayList的实现原理
- 深入java集合学习2-ArrayList的实现原理
- java的ArrayList的实现原理
- Java集合6:ArrayList的实现原理
- 深入Java集合学习系列:ArrayList的实现原理