Java集合之List


List继承自Collection的接口,List也是集合的一种。List是有序队列,List中的没一个元素都会有一个索引,第一个元素的索引是0,往后的元素的索引值依次+1,List中允许有重复的元素。
List框架:
Java集合之List
List接口源码:
[java] view plain copy
  1. public interface List<E> extends Collection<E> {  
  2.     int size();//大小  
  3.     boolean isEmpty();//判断是否为空  
  4.     boolean contains(Object o);//判断是否包含某个对象  
  5.     Iterator<E> iterator();//返回迭代对象  
  6.     Object[] toArray(); //返回对象数组  
  7.     <T> T[] toArray(T[] a);//对象数组  
  8.     boolean add(E e);//添加某个对象  
  9.     boolean remove(Object o);//删除某个对象  
  10.     boolean containsAll(Collection<?> c); // 是否包含某个Collection的所有对象  
  11.     boolean addAll(Collection<? extends E> c);//将Collection对象追加到List中  
  12.     boolean addAll(int index, Collection<? extends E> c);//在某个位置将Collection对象追加到List中  
  13.     boolean removeAll(Collection<?> c);//去掉Collection中所包含的对象  
  14.     boolean retainAll(Collection<?> c);//去掉不包含在Collection中所包含的对象  
  15.     default void replaceAll(UnaryOperator<E> operator) {  
  16.         Objects.requireNonNull(operator);  
  17.         final ListIterator<E> li = this.listIterator();  
  18.         while (li.hasNext()) {  
  19.             li.set(operator.apply(li.next()));  
  20.         }  
  21.     }  
  22.     @SuppressWarnings({"unchecked""rawtypes"})  
  23.     default void sort(Comparator<? super E> c) {  
  24.         Object[] a = this.toArray();  
  25.         Arrays.sort(a, (Comparator) c);  
  26.         ListIterator<E> i = this.listIterator();  
  27.         for (Object e : a) {  
  28.             i.next();  
  29.             i.set((E) e);  
  30.         }  
  31.     }  
  32.     void clear();//删除所有对象  
  33.     boolean equals(Object o);//判断两个list是否相同  
  34.     int hashCode();//返回List的hashCode  
  35.     E get(int index);//返回某个位置的对象  
  36.     E set(int index, E element);//替换某个位置的对象  
  37.     void add(int index, E element);//在某个位置添加对象  
  38.     E remove(int index);//删除某个位置的对象  
  39.     int indexOf(Object o);//返回某个对象在List中的位置  
  40.     int lastIndexOf(Object o);//List中最后一个对象的坐标  
  41.     ListIterator<E> listIterator();//返回整个List的迭代  
  42.     ListIterator<E> listIterator(int index); //从某个位置开始返回List的迭代  
  43.     List<E> subList(int fromIndex, int toIndex);//截取部分List  
  44.     @Override  
  45.     default Spliterator<E> spliterator() {  
  46.         return Spliterators.spliterator(this, Spliterator.ORDERED);  
  47.     }  
  48. }