常用容器(Collection)实现类总结(一)——ArrayList

ArrayList简略说明:

List 接口的大小可变数组的实现。实现了所有可选列表操作,并允许包括 null 在内的所有元素。除了实现 List 接口外,此类还提供一些方法来操作内部用来存储列表的数组的大小。

(Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list.)

每个 ArrayList 实例都有一个容量。该容量是指用来存储列表元素的数组的大小。它总是至少等于列表的大小。随着向 ArrayList 中不断添加元素,其容量也自动增长。

(Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. )

 

ArrayList优、缺点分析:

优点:

  • 线性有序存储,可重复存储
  • ArrayList是一种动态数组,首先在构造一个实例的时候,构造方法会给实例一个初始大小,当元素长度不够时,调用Arrays.copyOf方法,拷贝当前数组到一个新的长度更大的数组
  • 查找快且方便,查找某数据时通过下标直接获取元素

缺点:

  • 插入、增添、删除元素速率较低
  • 线程不安全(Vector是线程安全的,但是效率不如ArrayList)

 

基本实现:

 1 public class MyArrayList {
 2     private Object[] elementDate;
 3     
 4     private int size;
 5     
 6     public Object get(int index) {
 7         if(index<0 || index>=size) {
 8             try {
 9                 throw new Exception();
10             } catch (Exception e) {
11                 e.printStackTrace();
12             }
13         }
14         return elementDate[index];
15     }
16     
17     public boolean isEmpty() {
18         return size == 0;
19     }
20     
21     public int size() {
22         return size;
23     }
24     
25     public MyArrayList(){
26         this(10);          //调用有参构造方法,初始化数组大小为10
27     }
28     public MyArrayList(int initialCapacity){
29         if(initialCapacity<0){
30             try {
31                 throw new Exception();
32             } catch (Exception e) {
33                 e.printStackTrace();
34             }
35         }
36         elementDate = new Object[initialCapacity];
37     }
38     
39     public void add(Object obj) {
40         if(size==elementDate.length) {
41             Object[] newArrayList = new Object[size*2+1]; //当数组达到上限,创建新的数组对象
      //System.arraycopy(elementDate, 0, newArrayList, 0, elementDate.length);  //使用arraycopy方法也没问题,代码更简洁
42 for(int i=0; i<newArrayList.length; i++) { 43 newArrayList[i] = elementDate[i]; //遍历旧数组元素,存入新数组 44 elementDate = newArrayList; //将新数组地址传递给旧数组 45 } 46 } 47 elementDate[size++] = obj; 48 }

 

对于remove方法的详细说明:

  • remove方法可以根据索引值(index)或者指定对象删除元素:
    1     public void remove(int index) {
    2         rangeCheck(index);
    3         elementDate[index] = null;
    4         System.arraycopy(elementDate, index+1, elementDate, index, elementDate.length-(index+1));
    5         size--;
    6     }

    当出现索引越界时,调用方法rangeCheck,代码:

    1 public void rangeCheck(int index) {
    2         if(index < 0 || index >= this.size) {   
    3             try {
    4                 throw new Exception();
    5             }catch(Exception e) {
    6                 e.printStackTrace();
    7             }
    8         }
    9     }

     


  • remove方法还有个重载(override)方法:
    public boolean remove(Object o) {
            if (o == null) {
                for (int index = 0; index < size; index++)
                    if (elementData[index] == null) {
                        fastRemove(index);             //fastRemove方法类似于索引删除方法
                        return true;
                    }
            } else {
                for (int index = 0; index < size; index++)
                    if (o.equals(elementData[index])) {   //此处用的equals方法,为对象的值比较
                        fastRemove(index);
                        return true;            //这里return后返回boolean,所以只会删除第一次出现的对象        
                    }
            }
            return false;
        }

     

  • remove方法的测试结果:
     1     public static void main(String[] args) {
     2         ArrayList list = new ArrayList();
     3         list.add("aa");
     4         list.add("bb");
     5         list.add(new Date());
     6         list.add(new Date());
     7         list.remove(new Date());
     8         for(int i = 0; i < list.size(); i++) {
     9             System.out.println(list.get(i));
    10         }
    11     }

posted @ 2017-03-24 15:16  NOthingAJ  阅读(269)  评论(0编辑  收藏  举报