Fork me on Github

集合之ArrayList

ArrayList继承AbstractList抽象类


AbstractLis抽象类继承AbstractCollection抽象类并实现了List接口

AbstractCollection和List都实现了Collection接口

 

 

一、ArrayList常用方法

 

    • add(Object o)方法,用于向list集合容器之中添加元素。

 

    • add(int index,Object o)方法,在指定位置添加元素,原来位置的元素后置。

 

    • addAll(Collection c)方法,用于将集合c中的所有元素都添加到列表中。

 

    • addAll(int index,Collection c),index用于指定Collection的第一个元素所插入位置的索引。

 

    • size()方法,用于获取集合中有多少个元素。

 

    • get(int index)方法,获取指定索引(从0开始)位置的元素。

 

    • clear()方法,清空List集合中的所有元素。

 

    • isEmpty()方法,判断集合容器有没有元素。

 

    • contains(Object o)方法,用来判断集合容器中是否含参数元素。

 

    • containsAll(Collection<?> c)方法,判断是否包含参数集合的所有元素。

 

    • remove(Objiect o)方法,删除List集合元素,返回boolean,并让后面元素前移。

 

    • remove(int index)方法,移除此列表中指定位置的元素。

 

    • set(int index,Object o)方法,替换指定位置的元素。

 

    • sort()方法,用于列表排序,必要时重写Comparable接口中的方法。

 

二、ArrayList部分方法实现

 

1.add(Object o)

1    public boolean add(Object o) {
2         ensureCapacityInternal(size + 1);  // 扩容
3         elementData[size++] = o;
4         return true;
5    }

将指定元素附加到此列表的末尾,ensureCapacityInternal判断是否要扩容,变成原来最大容量的1.5倍+1。

 

2.add(int index,Object o)

1     public void add(int index, Object o) {
2         rangeCheckForAdd(index); //范围检查
3 
4         ensureCapacityInternal(size + 1);  
5         System.arraycopy(elementData, index, elementData, index + 1,
6                          size - index);  //将指定源数组中的数组从指定位置开始复制到目标数组的指定位置
7         elementData[index] = o;
8         size++;
9     }

在此列表中的指定位置插入指定元素。将当前位于该位置的元素(如果有)和任何后续元素向右移动(将其索引加一)。

 

3.remove(int index)

 1     public Object remove(int index) {
 2         rangeCheck(index);
 3 
 4         
5 Object oldValue = elementData(index); 6 7 int numMoved = size - index - 1; 8 if (numMoved > 0) 9 System.arraycopy(elementData, index+1, elementData, index, 10 numMoved); 11 elementData[--size] = null; 12 13 return oldValue; 14 }

移除此列表中指定位置的元素。将任何后续元素向左移动(从它们的索引中减去 1)。

 

4.remove(Object o)

 1     public boolean remove(Object o) {
 2         if (o == null) {
 3             for (int index = 0; index < size; index++)
 4                 if (elementData[index] == null) {
 5                     fastRemove(index);
 6                     return true;
 7                 }
 8         } else {
 9             for (int index = 0; index < size; index++)
10                 if (o.equals(elementData[index])) {
11                     fastRemove(index);
12                     return true;
13                 }
14         }
15         return false;
16     }

从此列表中删除第一次出现的指定元素(如果存在)。如果列表不包含该元素,则它不变。

 

5.contains(Object o)

 1     public boolean contains(Object o) {
 2         return indexOf(o) >= 0;
 3     }
 4  
 5     public int indexOf(Object o) {
 6         if (o == null) {
 7             for (int i = 0; i < size; i++)
 8                 if (elementData[i]==null)
 9                     return i;
10         } else {
11             for (int i = 0; i < size; i++)
12                 if (o.equals(elementData[i]))
13                     return i;
14         }
15         return -1;
16     }

如果此列表包含指定元素,则返回 true。

 

6.clear()

1     public void clear() {
2         
3         for (int i = 0; i < size; i++)
4             elementData[i] = null;
5 
6         size = 0;
7     }

从此列表中删除所有元素。此调用返回后,列表将为空。

posted @ 2022-02-09 10:50  昂昂呀  阅读(74)  评论(0)    收藏  举报