学习list

 

一.集合的基本结构图

二.list中提供的方法

boolean  add(Object o) 向列表的尾部追加指定的元素。
void   add(int index,Object o) 在列表的指定位置插入指定元素。
boolean addAll(Collection c) 追加指定collection中的所有元素到此列表的尾部,顺序      是指定collection的迭代器返回这些元素的顺序。
boolean addAll(int index,Collection c) 将指定collection中的所有元素添加到列表的       指定位置中。
void clear()  从列表中移除所有的元素。
boolean contains(Object) 如果列表包含指定的元素.
boolean contains(Collection c) 如果列表包含指定collection的所有元素,则返回true
boolean equals(Object o) 比较指定的对象与列表是否相等.
Object  get(int index) 返回列表中指定位置的元素.
int hashCode()  返回列表的哈希码.
int indexOf(Object o) 返回列表中首次出现指定元素的位置,如果无此元素,则返回-1
int lastIndexOf(Object o) 返回列表中最后一次出现指定元素的位置,如无,则返回-1
boolean isEmpty()  如果列表不包含元素,则返回true
Iterator iterator() 返回以正确顺序在列表的元素上进行迭代的迭代器。 
ListIterator listIterator() 返回列表中元素的列表迭代器(以正确顺序).
ListIterator listIterator(int index) 返回列表中元素的列表迭代器(以正确顺序),从列表       的指定位置开始.
boolean remove(Object o) 移除列表中首次出现的指定元素.
Object remove(int index) 移除列表中指定位置的元素(可选操作)。将所有的后续元素向     左移动(将其索引减 1)。返回从列表中移除的元素。返回的是     以前在指定位置的元素.
boolean removeAll(Collection c) 从列表中移除指定collection包含的所有元素.
boolean retainAll(Collection c) 仅在列表中保留指定collection所包含的元素.
Object  set(int index,Object element) 用指定元素替换列表中指定位置的元素.返回的是        以前在指定位置的元素.
int size() 返回列表中的元素数.
List subList(int fromindex,int toindex) 返回列表中指定的fromindex(包含)和toindex          (不包含)之间的部分视图.
Object[]  toArray() 返回以正确顺序包含列表中的所有元素的数组.

三.下面说一说Aarraylist和linkedlist

  1.AarrayList:

    对于ArrayList而言,它实现List接口、底层使用数组保存所有元素。其操作基本上是对数组的操作。下面我们来分析ArrayList的源代码:

  1)底层使用数组实现

  

private transient Object[] elementData; 

   2)构造方法:

ArrayList提供了三种方式的构造器,可以构造一个默认初始容量为10的空列表、构造一个指定初始容量的空列表以及构造一个包含指定collection的元素的列表,这些元素按照该collection的迭代器返回它们的顺序排列的。

public ArrayList() {  
    this(10);  
}  
  
public ArrayList(int initialCapacity) {  
    super();  
    if (initialCapacity < 0)  
        throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);  
    this.elementData = new Object[initialCapacity];  
}  
  
public ArrayList(Collection<? extends E> c) {  
    elementData = c.toArray();  
    size = elementData.length;  
    // c.toArray might (incorrectly) not return Object[] (see 6260652)  
    if (elementData.getClass() != Object[].class)  
        elementData = Arrays.copyOf(elementData, size, Object[].class);  

3)存储

ArrayList提供了set(int index, E element)、add(E e)、add(int index, E element)、addAll(Collection<? extends E> c)、addAll(int index, Collection<? extends E> c)这些添加元素的方法。下面我们一一讲解:

// 用指定的元素替代此列表中指定位置上的元素,并返回以前位于该位置上的元素。  
public E set(int index, E element) {  
    RangeCheck(index);  
  
    E oldValue = (E) elementData[index];  
    elementData[index] = element;  
    return oldValue;  
}  
// 将指定的元素添加到此列表的尾部。  
public boolean add(E e) {  
    ensureCapacity(size + 1);   
    elementData[size++] = e;  
    return true;  
}  
// 将指定的元素插入此列表中的指定位置。  
// 如果当前位置有元素,则向右移动当前位于该位置的元素以及所有后续元素(将其索引加1)。  
public void add(int index, E element) {  
    if (index > size || index < 0)  
        throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);  
    // 如果数组长度不足,将进行扩容。  
    ensureCapacity(size+1);  // Increments modCount!!  
    // 将 elementData中从Index位置开始、长度为size-index的元素,  
    // 拷贝到从下标为index+1位置开始的新的elementData数组中。  
    // 即将当前位于该位置的元素以及所有后续元素右移一个位置。  
    System.arraycopy(elementData, index, elementData, index + 1, size - index);  
    elementData[index] = element;  
    size++;  
}  
// 按照指定collection的迭代器所返回的元素顺序,将该collection中的所有元素添加到此列表的尾部。  
public boolean addAll(Collection<? extends E> c) {  
    Object[] a = c.toArray();  
    int numNew = a.length;  
    ensureCapacity(size + numNew);  // Increments modCount  
    System.arraycopy(a, 0, elementData, size, numNew);  
    size += numNew;  
    return numNew != 0;  
}  
// 从指定的位置开始,将指定collection中的所有元素插入到此列表中。  
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);  // Increments modCount  
  
    int numMoved = size - index;  
    if (numMoved > 0)  
        System.arraycopy(elementData, index, elementData, index + numNew, numMoved);  
  
    System.arraycopy(a, 0, elementData, index, numNew);  
    size += numNew;  
    return numNew != 0;  
} 

https://blog.csdn.net/u010305706/article/details/51007826

猜你喜欢

转载自www.cnblogs.com/strugglecola/p/8889216.html