您的位置:首页 > 编程语言 > Java开发

JAVA集合二 ——list(01、List接口)

2017-04-21 10:00 337 查看


Java-集合之List

     特别声明:

     *本文只是备忘录。

     

    


     

      List是一种有序的集合,允许list中对象重复和null的存在(根据实现类不同而限制不一样)。并且提供索引对list中的对象进行访问,新增,操作,删除。 

      #List中的迭代器:

/**
* Returns a list iterator over the elements in this list (in proper
* sequence).
*
* @return a list iterator over the elements in this list (in proper
*         sequence)
*/
ListIterator<E> listIterator();

/**
* Returns a list iterator over the elements in this list (in proper
* sequence), starting at the specified position in the list.
* The specified index indicates the first element that would be
* returned by an initial call to {@link ListIterator#next next}.
* An initial call to {@link ListIterator#previous previous} would
* return the element with the specified index minus one.
*
* @param index index of the first element to be returned from the
*        list iterator (by a call to {@link ListIterator#next next})
* @return a list iterator over the elements in this list (in proper
*         sequence), starting at the specified position in the list
* @throws IndexOutOfBoundsException if the index is out of range
*         ({@code index < 0 || index > size()})
*/
ListIterator<E> listIterator(int index);
/**
* Returns an iterator over the elements in this list in proper sequence.
*
* @return an iterator over the elements in this list in proper sequence
*/
Iterator<E> iterator();

    我们可以看到list中的迭代器 不仅有Iterator还有ListIterator。

    Iterator上篇已经说过,这次来看看 ListIterator。

   首先看看这个方法:

/**
* Returns a list iterator over the elements in this list (in proper
* sequence), starting at the specified position in the list.
* The specified index indicates the first element that would be
* returned by an initial call to {@link ListIterator#next next}.
* An initial call to {@link ListIterator#previous previous} would
* return the element with the specified index minus one.
*
* @param index index of the first element to be returned from the
*        list iterator (by a call to {@link ListIterator#next next})
* @return a list iterator over the elements in this list (in proper
*         sequence), starting at the specified position in the list
* @throws IndexOutOfBoundsException if the index is out of range
*         ({@code index < 0 || index > size()})
*/
ListIterator<E> listIterator(int index);

   该方法返回了一个ListIterator,并且通过传入的index来指定第一次调用next()返回的元素位置。简单说就是指定了锚点。

   这样的方法对与集合中使用lastIndexof(Obj o)是很有用的。定位元素最后出现的位置,冲后到前的查找。使用ListIterator会很方便。

    ListIterator源码如下:

public interface ListIterator<E> extends Iterator<E> {
// Query Operations

boolean hasNext();

E next();

/**
* Returns {@code true} if this list iterator has more elements when
* traversing the list in the reverse direction.  (In other words,
* returns {@code true} if {@link #previous} would return an element
* rather than throwing an exception.)
*
* @return {@code true} if the list iterator has more elements when
*         traversing the list in the reverse direction
*/
boolean hasPrevious();

/**
* Returns the previous element in the list and moves the cursor
* position backwards.  This method may be called repeatedly to
* iterate through the list backwards, or intermixed with calls to
* {@link #next} to go back and forth.  (Note that alternating calls
* to {@code next} and {@code previous} will return the same
* element repeatedly.)
*
* @return the previous element in the list
* @throws NoSuchElementException if the iteration has no previous
*         element
*/
E previous();

/**
* Returns the index of the element that would be returned by a
* subsequent call to {@link #next}. (Returns list size if the list
* iterator is at the end of the list.)
*
* @return the index of the element that would be returned by a
*         subsequent call to {@code next}, or list size if the list
*         iterator is at the end of the list
*/
int nextIndex();

/**
* Returns the index of the element that would be returned by a
* subsequent call to {@link #previous}. (Returns -1 if the list
* iterator is at the beginning of the list.)
*
* @return the index of the element that would be returned by a
*         subsequent call to {@code previous}, or -1 if the list
*         iterator is at the beginning of the list
*/
int previousIndex();

// Modification Operations

/**
* Removes from the list the last element that was returned by {@link
* #next} or {@link #previous} (optional operation).  This call can
* only be made once per call to {@code next} or {@code previous}.
* It can be made only if {@link #add} has not been
* called after the last call to {@code next} or {@code previous}.
*
* @throws UnsupportedOperationException if the {@code remove}
*         operation is not supported by this list iterator
* @throws IllegalStateException if neither {@code next} nor
*         {@code previous} have been called, or {@code remove} or
*         {@code add} have been called after the last call to
*         {@code next} or {@code previous}
*/
void remove();

/**
* Replaces the last element returned by {@link #next} or
* {@link #previous} with the specified element (optional operation).
* This call can be made only if neither {@link #remove} nor {@link
* #add} have been called after the last call to {@code next} or
* {@code previous}.
*
* @param e the element with which to replace the last element returned by
*          {@code next} or {@code previous}
* @throws UnsupportedOperationException if the {@code set} operation
*         is not supported by this list iterator
* @throws ClassCastException if the class of the specified element
*         prevents it from being added to this list
* @throws IllegalArgumentException if some aspect of the specified
*         element prevents it from being added to this list
* @throws IllegalStateException if neither {@code next} nor
*         {@code previous} have been called, or {@code remove} or
*         {@code add} have been called after the last call to
*         {@code next} or {@code previous}
*/
void set(E e);

/**
* Inserts the specified element into the list (optional operation).
* The element is inserted immediately before the element that
* would be returned by {@link #next}, if any, and after the element
* that would be returned by {@link #previous}, if any.  (If the
* list contains no elements, the new element becomes the sole element
* on the list.)  The new element is inserted before the implicit
* cursor: a subsequent call to {@code next} would be unaffected, and a
* subsequent call to {@code previous} would return the new element.
* (This call increases by one the value that would be returned by a
* call to {@code nextIndex} or {@code previousIndex}.)
*
* @param e the element to insert
* @throws UnsupportedOperationException if the {@code add} method is
*         not supported by this list iterator
* @throws ClassCastException if the class of the specified element
*         prevents it from being added to this list
* @throws IllegalArgumentException if some aspect of this element
*         prevents it from being added to this list
*/
void add(E e);
}

  ListIterator扩展了Iterator接口,提供了向前获取元素的方法。     

其中set(E e)方法是替换上一次通过next()或者previous()方法获取的元素。该方法只能在next()或者previous()调用之后使用,使用之前不能调用add(E e)或者remove();

      add(E e)在当前位置之前插入一个新元素。锚点+1,next()方法不受影响,previous()方法会获取新加入的元素。

      仔细想象下结构,就能明白。

List的几个实现类的对比:
ArrayList:可以当作是对寄出数组的封装,改变List中的对象(插入、删除)比较慢,索引快。应为改变会导致数组的copy,索引直接通过数组的索引。扩容的时候增加当前数组长度的一半。
Vector:  是一种线程安全的ArrayList。可以指定每次扩容的大小,如果不指定,每次扩容,数组长度加一倍。
LinkedList: 双向链表,内部数据结构保存当前对象,和当前对象之前和之后的对象。这样插入很快,但是设计到索引很慢。
Stack:  继承与Vector,实现了一种FILO(先进后出)的方式。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  JAVA 集合 java list
相关文章推荐