Java-List源碼分析

來源:互聯網
上載者:User

標籤:java list

List繼承自Collection的介面,List也是集合的一種。List是有序隊列,List中的沒一個元素都會有一個索引,第一個元素的索引是0,往後的元素的索引值依次+1,List中允許有重複的元素。List介面源碼:
public interface List<E> extends Collection<E> {    int size();//大小    boolean isEmpty();//判斷是否為空白    boolean contains(Object o);//判斷是否包含某個對象    Iterator<E> iterator();//返回迭代對象    Object[] toArray(); //返回對象數組    <T> T[] toArray(T[] a);//對象數組    boolean add(E e);//添加某個對象    boolean remove(Object o);//刪除某個對象    boolean containsAll(Collection<?> c); // 是否包含某個Collection的所有對象    boolean addAll(Collection<? extends E> c);//將Collection對象追加到List中    boolean addAll(int index, Collection<? extends E> c);//在某個位置將Collection對象追加到List中    boolean removeAll(Collection<?> c);//去掉Collection中所包含的對象    boolean retainAll(Collection<?> c);//去掉不包含在Collection中所包含的對象    default void replaceAll(UnaryOperator<E> operator) {        Objects.requireNonNull(operator);        final ListIterator<E> li = this.listIterator();        while (li.hasNext()) {            li.set(operator.apply(li.next()));        }    }    @SuppressWarnings({"unchecked", "rawtypes"})    default void sort(Comparator<? super E> c) {        Object[] a = this.toArray();        Arrays.sort(a, (Comparator) c);        ListIterator<E> i = this.listIterator();        for (Object e : a) {            i.next();            i.set((E) e);        }    }    void clear();//刪除所有對象    boolean equals(Object o);//判斷兩個list是否相同    int hashCode();//返回List的hashCode    E get(int index);//返回某個位置的對象    E set(int index, E element);//替換某個位置的對象    void add(int index, E element);//在某個位置添加對象    E remove(int index);//刪除某個位置的對象    int indexOf(Object o);//返回某個對象在List中的位置    int lastIndexOf(Object o);//List中最後一個對象的座標    ListIterator<E> listIterator();//返回整個List的迭代    ListIterator<E> listIterator(int index); //從某個位置開始返回List的迭代    List<E> subList(int fromIndex, int toIndex);//截取部分List    @Override    default Spliterator<E> spliterator() {        return Spliterators.spliterator(this, Spliterator.ORDERED);    }}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Java-List源碼分析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.