List和ArrayList

table of Contents

List

Here Insert Picture Description
Common methods:

method Explanation
public void add(int index,E element) Adding data at the specified index position
public E get(int index) To obtain data on the specified index
public int indexOf(Object o) Find the specified object index position
public ListIterator listIterator () Get ListIterator interface instance
public static List of(E …element) Create a List collection by specifying the content
public E set(int index,E element) Modifying data on the specified index
default void sort(Comparator<? super E> c) List sorted achieve the set
List<String> list = List.of();			//使用of创建的List数组可以读,不可以改。(JDK1.9新增)
List<String> listA = new ArrayList<>();	//使用子类向上转型可以修改数据
List<String> listB = new LinkedList<>();//使用子类向上转型可以修改数据
List<String> listC = new Vector<>(); 	//使用子类向上转型可以修改数据

ArrayList

ArrayList array implemented in the form of use, according to the index can query the data index.

Here Insert Picture Description
ArrayList constructor with no arguments Source:

public ArrayList() {
        this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;	//默认初始容量为0
    }

private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = new Object[0];

There ArrayList argument constructor Source:

public ArrayList(int initialCapacity) {
        if (initialCapacity > 0) {
       this.elementData = new Object[initialCapacity];//大于0开辟一个传入数据大小的Object的数组
        } else {
            if (initialCapacity != 0) {
              throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity);
            }
            this.elementData = EMPTY_ELEMENTDATA;	//输入0,和无参构造一样
        }
    }

private static final Object[] EMPTY_ELEMENTDATA = new Object[0];

When using add (), addAll () operation to increase the data expansion is performed.
Use within 10 or 10 no-argument constructor is better, with more than 10 parameters have constructed or will produce garbage space.
add the source of expansion method:

public boolean add(E e) {	
        ++this.modCount; //线程保护
        this.add(e, this.elementData, this.size);	
        return true;
    }

private void add(E e, Object[] elementData, int s) {
        if (s == elementData.length) {	//判断数据的容量是否满了
            elementData = this.grow();	//容量扩充
        }

        elementData[s] = e;
        this.size = s + 1;
    }

    private Object[] grow() {
        return this.grow(this.size + 1);
    }

private Object[] grow(int minCapacity) {
        return this.elementData = Arrays.copyOf(this.elementData, this.newCapacity(minCapacity));	//copyOf改变ArrayList的容量赋给新的ArrayList数组
    }

//数组容量扩充
private int newCapacity(int minCapacity) {		
   int oldCapacity = this.elementData.length;  //获取前数组的长度
   int newCapacity = oldCapacity + (oldCapacity >> 1); //旧数据+新数据(旧数据右移1,相当于除2)
        if (newCapacity - minCapacity <= 0) {
            if (this.elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
                return Math.max(10, minCapacity);	//第一次增加数据时ArrayList的容量变为10
            } else if (minCapacity < 0) {
                throw new OutOfMemoryError();
            } else {
                return minCapacity;
            }
        } else {
            return newCapacity - 2147483639 <= 0 ? newCapacity : hugeCapacity(minCapacity);
        }
    }

ArrayList expansion steps:
. 1, when the ArrayList capacity is not specified, the default is 0, and it can be said capacity of 10 .

2, first increases when the data length is determined == size ArrayList performs expansion operation.

3, using Arrays.copyOf () to retain the original contents of the array simultaneously array expansion, the capacity of changing the length.

4, in newCapacity () within, to operate.
     1) Get the array length.
     2) Get a new length newCapacity = (oldCapacity + (Capacity >> 1)); 1 is divided by 2 to the right.

5, a new determination of the length of - (incoming size + 1) <= 0.

    1) determines whether the array capacity is 0, 0 is returned Math.max (10, passed size + 1 value); Returns the largest of the two numbers, the first return 10.

2) is smaller than 0, error.

     3) is greater than 0, returns the value passed in size + 1.

6, it is determined (new length - (the incoming size + 1)> 0) newCapacity - 2147483639 (the maximum length of the array) <= 0 newCapacity: hugeCapacity (passed value size + 1);?

LinkedList

LinkedList realized using a linked-list data storage, but the performance of the query is not high.
Here Insert Picture Description

Vector

Vector and ArrayList achieve similar, but the utilization rate is very low.
Here Insert Picture Description
Vector default capacity of 10:

 public Vector() {
        this(10);
    }

the difference

ArrayList: In JDK1.2 proposed to add collections, storage arrays implemented based on dynamic data, according to the index query time complexity is O (1), the internal thread synchronization is not used, a non-thread safe.

LinkedList: Presented at JDK1.2 added collection classes, based on the linked list for data storage, according to the time index query complexity is O (n).

Vector: In a class JDK1.0 proposed in JDK1.2 added collections, storage arrays implemented based on dynamic data, the internal synchronization methods of using the synchronized belongs thread safe.

Published 61 original articles · won praise 0 · Views 2183

Guess you like

Origin blog.csdn.net/sabstarb/article/details/104584363