java集合类详解--ArrayList
2017-02-14 10:35
465 查看
前言
版本说明:jdk1.6正文
ArrayList实现了List接口,并继承AbstractList抽象类。AbstractList抽象类实现了List的接口中的关于iterator()、listIterator(),以及基本的add()、remove()、set()方法。
ArrayList中还是重写了AbstractList抽象类中的add()和remove()和set()方法,并实现了get()方法。
ArrayList内部维护了一个Object的数组,ArrayList中增删改查的元素都是通过这个数组来操作的。查看源码了解到这个数组是一个transient属性,
private transient Object[] elementData;
关于transient的作用可以简单理解为在存在序列化的操作的时候只会序列化这个数组中有数据的元素,java中的数组一旦声明并指定数组长度之后,是无法再对其进行扩容的,这也是数组能够快速查找元素的原因,但是在ArrayList中既然维护了这个数组,那么怎么才能保证add()方法在数组的容量达到最大还能够正常执行呢?继续看源码,发现了有一个ensureCapacity数组扩容的方法
public void ensureCapacity(int minCapacity) { modCount++; int oldCapacity = elementData.length; //minCapacity的值一般为数组中实际元素的个数+即将添加的元素的个数 if (minCapacity > oldCapacity) { Object oldData[] = elementData; int newCapacity = (oldCapacity * 3)/2 + 1; if (newCapacity < minCapacity) newCapacity = minCapacity; elementData = Arrays.copyOf(elementData, newCapacity); } }
这个方法的流程就是将元素组中的元素copy到新数组中,这个新数组的大小默认为元素组大小的1.5倍+1,如果这个大小还是比给定的”最小容量”小,那么新数组的大小就是方法传进来的这个”最小容量”,这样”扩容后”数组的大小就保证了add()方法的正确执行。
增加元素:我们只介绍add(int index,E element),addAll(int index,Collection
public void add(int index, E element) { if (index > size || index < 0) throw new IndexOutOfBoundsException( "Index: "+index+", Size: "+size); //确保数组大小,并且将mod次数加1 ensureCapacity(size+1); //将自index位置(包含index)以后的数据复制到的序号+1的位置(后移一位) System.arraycopy(elementData, index, elementData, index + 1, size - index); //给数组中的index位的元素赋值 elementData[index] = element; //size表示数组中实际包含的元素个数,不是elementData数组的大小 size++; }
上面的方法中只是插入了一个元素,就有可能造成数组的扩容,而且System.arraycopy()这个方法所耗费的时间与index的大小成反比,与elementData的实际元素个数成正比
public boolean addAll(int index, Collection<? extends E> c) { if (index > size || index < 0) throw new IndexOutOfBoundsException( "Index: " + index + ", Size: " + size); Object[] a = c.toArray(); int numNew = a.length; ensureCapacity(size + numNew); int numMoved = size - index; //如果c中的元素个数不为零 if (numMoved > 0) //将原数组自index起的数据向后移numMoved个位置 System.arraycopy(elementData, index, elementData, index + numNew, numMoved); //将a数组即c集合中的元素复制到elementData数组中 System.arraycopy(a, 0, elementData, index, numNew); size += numNew; return numNew != 0; }
ArrayList的get()方法直接按照数组[索引]的数据,所以很快,并且耗费的时间固定
public E get(int index) { RangeCheck(index); return (E) elementData[index]; }
ArrayList修改元素的方法set()与get()比较类似,这里不表
ArrayList删除指定下标的元素remove(int index),源码如下:
public E remove(int index) { RangeCheck(index); modCount++; E oldValue = (E) elementData[index]; int numMoved = size - index - 1; if (numMoved > 0) //将index下标后面的元素往前移一位 System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--size] = null; // Let gc do its work return oldValue; }
remove还有个重载方法remove(Object o),源码如下:
public boolean remove(Object o) { //ArrayList中允许插入null元素,可以看到当会有多个的时候只会remove掉第一个(正序) if (o == null) { for (int index = 0; index < size; index++) if (elementData[index] == null) { fastRemove(index); return true; } } else { /** * 允许插入相同的元素,如果remove,只会删掉正序的第一个。 * 如果添加的元素没有重写equals和hashCode方法,则会调用Object.equals(),它是直接比对的引用是否相同 */ for (int index = 0; index < size; index++) if (o.equals(elementData[index])) { fastRemove(index); return true; } } return false; }
总结
因为ArrayList中维护的是一个不可变的object数组,所以查询和修改数据很快,而插入和删除的效率跟插入的位置、插入的个数、原数组的实际元素个数有关,位置索引越靠后,插入个数少,实际元素个数少,这样效率会很快。相关文章推荐
- Java集合类ArrayList、LinkedList、vector、SynchronizedList详解
- 【Java集合类】ArrayList详解 (JDK7)
- Java集合类详解
- ArrayList详解
- Java集合类2—ArrayList、LinkList类
- Java集合类详解
- Java中 ArrayList、Vector和LinkedList 的使用和详解!
- 17-4:ArrayList的contains/remove对对象操作关联equals的详解
- C#集合类详解,Hashtable 和 SortedList 类,Dictionary 和 SortedLi,Array ,ArrayList ,List ,Stack,SortedDictionary ,ListDictionary
- Vector、ArrayList、List详解
- Java集合类ArrayList循环中删除特定元素
- Java集合类详解
- C#集合类详解,Hashtable 和 SortedList 类,Dictionary 和 SortedLi,Array ,ArrayList ,List ,Stack,SortedDictionary ,ListDictionary ,StringDictionary
- C# IList, ArrayList与List的区别详解 & 简单用法举例
- Java集合类详解
- C# IList, ArrayList与List的区别详解 & 简单用法举例(转)
- ArrayList详解
- java集合类(三)Arraylist实例
- C# IList, ArrayList与List的区别详解 & 简单用法举例(转)
- Java集合排序及java集合类详解