Java ArrayList

Java ArrayList的構造方法和方法html

  • Constructor Summary

    Constructors
    Constructor Description
    ArrayList()
    Constructs an empty list with an initial capacity of ten.
    ArrayList​(int initialCapacity)
    Constructs an empty list with the specified initial capacity.
    ArrayList​(Collection<? extendsE> c)
    Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
  • Method Summary

    All MethodsInstance MethodsConcrete Methods
    Modifier and Type Method Description
    void add​(int index,E element)
    Inserts the specified element at the specified position in this list.
    boolean add​(E e)
    Appends the specified element to the end of this list.
    boolean addAll​(int index,Collection<? extendsE> c)
    Inserts all of the elements in the specified collection into this list, starting at the specified position.
    boolean addAll​(Collection<? extends E> c)
    Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator.
    void clear()
    Removes all of the elements from this list.
    Object clone()
    Returns a shallow copy of this  ArrayList instance.
    boolean contains​(Object o)
    Returns  true if this list contains the specified element.
    void ensureCapacity​(int minCapacity)
    Increases the capacity of this  ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
    void forEach​(Consumer<? super E> action)
    Performs the given action for each element of the  Iterable until all elements have been processed or the action throws an exception.
    E get​(int index)
    Returns the element at the specified position in this list.
    int indexOf​(Object o)
    Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
    boolean isEmpty()
    Returns  true if this list contains no elements.
    Iterator<E> iterator()
    Returns an iterator over the elements in this list in proper sequence.
    int lastIndexOf​(Object o)
    Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
    ListIterator<E> listIterator()
    Returns a list iterator over the elements in this list (in proper sequence).
    ListIterator<E> listIterator​(int index)
    Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
    E remove​(int index)
    Removes the element at the specified position in this list.
    boolean remove​(Object o)
    Removes the first occurrence of the specified element from this list, if it is present.
    boolean removeAll​(Collection<?> c)
    Removes from this list all of its elements that are contained in the specified collection.
    boolean removeIf​(Predicate<? super E> filter)
    Removes all of the elements of this collection that satisfy the given predicate.
    protected void removeRange​(int fromIndex, int toIndex)
    Removes from this list all of the elements whose index is between  fromIndex, inclusive, and  toIndex, exclusive.
    boolean retainAll​(Collection<?> c)
    Retains only the elements in this list that are contained in the specified collection.
    E set​(int index,E element)
    Replaces the element at the specified position in this list with the specified element.
    int size()
    Returns the number of elements in this list.
    Spliterator<E> spliterator()
    Creates a  late-binding and  fail-fast  Spliterator over the elements in this list.
    List<E> subList​(int fromIndex, int toIndex)
    Returns a view of the portion of this list between the specified  fromIndex, inclusive, and  toIndex, exclusive.
    Object[] toArray()
    Returns an array containing all of the elements in this list in proper sequence (from first to last element).
    <T> T[] toArray​(T[] a)
    Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.
    void trimToSize()
    Trims the capacity of this  ArrayList instance to be the list's current size.

 

 

解析ArrayList的構造函數java

// 默認構造函數,默認容量大小爲10
ArrayList()

// capacity是ArrayList的默認容量大小。每次擴容爲原來的1.5倍。
ArrayList(int capacity)

// 建立一個包含collection的ArrayList,能夠將別的ArrayList數組複製進去
ArrayList(Collection<? extends E> collection)

ArrayList適合查詢與修改,格局位置索引index查詢和修改只須要花費常數時間api

get(int index)數組

set(int index, E element)oracle

 

可是對ArrayList的插入,刪除是很是耗時的,除非是在數組末端函數

add(int index,E element)this

remove​(int index)//刪除索引位置的元素spa

remove(Obiect o)//尋找等於 o 的元素,其實就是每一個元素都比較一下,因此若是是自定義類就必需重寫equals方法code

 

因此咱們知道了ArrayList不適合頻繁插入刪除的場景,適合根據索引頻繁查詢修改的場景orm

 

ArrayList轉數組

toArray()//默認返回一個Object數組(注意!不能夠(String[])toArray(),你只能對一個個元素轉型不能對一個數組總體轉型,因此建議使用第二種方法 )

toArray(T[] a)//本身設置數組類型好比toArray(new String[list.size()])就是返回一個數組大小=list長度元素類型爲String的數組

 

ArrayList擴容

隨着元素不斷增長,原來空間不夠用的時候

ensureCapacityInternal(int minCapacity)//根據傳入的最小須要容量minCapacity來和數組的容量長度對比,若minCapactity大於或等於數組容量,則須要進行擴容。(若是實際存儲數組是空數組,則最小須要容量就是默認容量)

相關文章
相關標籤/搜索