`
chenfengbin
  • 浏览: 961 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

Java集合框架_学习笔记1(ArrayList篇)

阅读更多
根据这分Java collection framework
从Collection到List,Set,Map这些都是定义的集合接口:
现在从ArrayList的实现开始说起。
对代码分析:
a)构造方法:ArrayList()有三个方法,第一个,构造一个默认初始容量为10的空列表;第二个,构造一个指定
初始容量的空列表;第三个,构造一个包含指定collection的元素的列表。这些元素都会按照collection的迭代
器返回它们的顺序排列的。(代码如下)


1.public ArrayList() {   
2.    this(10);   
3.}   
4.
5.public ArrayList(int initialCapacity) {   
6.    super();   
7.    if (initialCapacity < 0)   
8.       throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);   
9.    this.elementData = new Object[initialCapacity];   
10.}   
11.  
12.public ArrayList(Collection<? extends E> c) {   
13.    elementData = c.toArray();   
14.    size = elementData.length;   
15.    // c.toArray might (incorrectly) not return Object[] (see 6260652)   
16.    if (elementData.getClass() != Object[].class)   
17.        elementData = Arrays.copyOf(elementData, size, Object[].class);   
18.}  


b)存储:ArrayList提供了set(int index, Object element)、add(Object  e)、add(int index, Object  element)、
addAll(Collection<? extends E> c)、addAll(int index, Collection<? extends E> c)这些添加元素的方法

NO.1

1.// 用指定的元素替代此列表中指定位置上的元素,并返回以前位于该位置上的元素。
2.public Object  set(int index, Object  element) {   
3.    RangeCheck(index);   
4.
5.    Object  oldValue =  elementData[index];
6.    elementData[index] = element;   
7.return oldValue;   
8.}  



NO.2

1.// 将指定的元素添加到此列表的尾部。
2.public boolean add(Object e) {   
3.    ensureCapacity(size + 1);     //改变ArrayList的大小
4.    elementData[size++] = e;   
5.return true;   
6.} 


  NO.3
1.// 将指定的元素插入此列表中的指定位置。
2.// 如果当前位置有元素,则向右移动当前位于该位置的元素以及所有后续元素(将其索引加1)。
3.public void add(int index, E element) {   
4.if (index > size || index < 0)   
5.   throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);   
6.      ensureCapacity(size+1);  //改变ArrayList的大小
7.// 将 elementData中从Index位置开始、长度为size-index的元素,
8.// 拷贝到从下标为index+1位置开始的新的elementData数组中。
9.// 即将当前位于该位置的元素以及所有后续元素右移一个位置。
10.    System.arraycopy(elementData, index, elementData, index + 1, size - index);   
11.    elementData[index] = element;   
12.    size++;   
13.}  


   NO.4
1.// 按照指定collection的迭代器所返回的元素顺序,将该collection中的所有元素添加到此列表的尾部。
2. public boolean addAll(Collection c) {   
3.    Object[] a = c.toArray();   
4.    int numNew = a.length;   
5.    ensureCapacity(size + numNew);  // Increments modCount     改变ArrayList的大小
6.    System.arraycopy(a, 0, elementData, size, numNew);   
7.    size += numNew;   
8.   return numNew != 0;   
9.}  

  NO.5
1.// 从指定的位置开始,将指定collection中的所有元素插入到此列表中。
2. public boolean addAll(int index, Collection c) {   
3.      if (index > size || index < 0)   
4.           throw new IndexOutOfBoundsException(   
5.                      "Index: " + index + ", Size: " + size);   
6.
7.          Object[] a = c.toArray();   
8.    int numNew = a.length;   
9.    ensureCapacity(size + numNew);  // Increments modCount   
10.
11.   int numMoved = size - index;   
12.   if (numMoved > 0)   
13.   System.arraycopy(elementData, index, elementData, index + numNew, numMoved);   
14.
15.   System.arraycopy(a, 0, elementData, index, numNew);   
16.   size += numNew;   
17.   return numNew != 0;   
18.}  

从  以上的操作应该可以看出,如果在做插入时,最好一次性插入,因为每一次

新加的时候,都要调用ensureCapacity方法来改变ArrayList的大小。



c)读取:

1.// 返回此列表中指定位置上的元素。
2.public Object get(int index) {   
3.    RangeCheck(index);   
4.
5.    return (Object) elementData[index];   
6.}  



d)删除:ArrayList提供了根据下标或者指定对象两种方式的删除功能。如下:
1.// 移除此列表中指定位置上的元素。
2.public E remove(int index) {   
3.    RangeCheck(index);   
4.
5.    modCount++;   
6.    E oldValue = (E) elementData[index];   
7. 
8.    int numMoved = size - index - 1;   
9.   if (numMoved > 0)   
10.        System.arraycopy(elementData, index+1, elementData, index, numMoved);   
11.        elementData[--size] = null; // Let gc do its work   
12.
13.        return oldValue;   
14.}  

  以下的这个方法是JDK1.5版本才有的  

1.// 移除此列表中首次出现的指定元素(如果存在)。这是应为ArrayList中允许存放重复的元素。\
2.public boolean remove(Object o) {   
3.// 由于ArrayList中允许存放null,因此下面通过两种情况来分别处理。
4.    if (o == null) {   
5.        for (int index = 0; index < size; index++)   
6.              if (elementData[index] == null) {   
7. // 类似remove(int index),移除列表中指定位置上的元素。
8.                   fastRemove(index);   
9.                   return true;   
10.            }   
11.   } else {   
12.          for (int index = 0; index < size; index++)   
13.                if (o.equals(elementData[index])) {   
14.                       fastRemove(index);   
15.                       return true;   
16.        }   
17.    }   
18.   return false;   
19.}  

e)调整数组容量:
        从上面介绍的向ArrayList中存储元素的代码中,我们看到,每当向数组中添加元素时,都要去检查添加后元
素的个数是否会超出当前数组的长度,如果超出,数组将会进行扩容,以满足添加数据的需求。数组扩容通过一个公开的方法ensureCapacity(int minCapacity)来实现。在实际添加大量元素前,我也可以使用ensureCapacity来手动增加ArrayList实例的容量,以减少递增式再分配的数量。



Java代码
1.public void ensureCapacity(int minCapacity) {   
2.     modCount++;   
3.     int oldCapacity = elementData.length;   
4.     if (minCapacity > oldCapacity) {   
5.          Object oldData[] = elementData;   
6.      int newCapacity = (oldCapacity * 3)/2 + 1;   
7.      if (newCapacity < minCapacity)   
8.                newCapacity = minCapacity;   
9.          // minCapacity is usually close to size, so this is a win:   
10.       elementData = Arrays.copyOf(elementData, newCapacity);   
11.    }   
12.}  



    从上述代码中可以看出,数组进行扩容时,会将老数组中的元素重新拷贝一份到新的数组中,每次数组容
量的增长大约是其原容量的1.5倍。这种操作的代价是很高的,因此在实际使用时,我们应该尽量避免数组容量
的扩张。当我们可预知要保存的元素的多少时,要在构造ArrayList实例时,就指定其容量,以避免数组扩容的
发生。或者根据实际需求,通过调用ensureCapacity方法来手动增加ArrayList实例的容量。



与以上代码相反,ArrayList还给我们提供了将底层数组的容量调整为当前列表保存的实际元素的大小的功能。
它可以通过trimToSize方法来实现。代码如下:


1.public void trimToSize() {   
2.    modCount++;   
3.    int oldCapacity = elementData.length;   
4.    if (size < oldCapacity) {   
5.         elementData = Arrays.copyOf(elementData, size);   
6.     }   
7.}  
  • 大小: 46.1 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics