標籤: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源碼分析