JAVA集合二 ——list(01、List接口)
2017-04-21 10:00
337 查看
Java-集合之List
特别声明:
*本文只是备忘录。
List是一种有序的集合,允许list中对象重复和null的存在(根据实现类不同而限制不一样)。并且提供索引对list中的对象进行访问,新增,操作,删除。
#List中的迭代器:
我们可以看到list中的迭代器 不仅有Iterator还有ListIterator。
Iterator上篇已经说过,这次来看看 ListIterator。
首先看看这个方法:
该方法返回了一个ListIterator,并且通过传入的index来指定第一次调用next()返回的元素位置。简单说就是指定了锚点。
这样的方法对与集合中使用lastIndexof(Obj o)是很有用的。定位元素最后出现的位置,冲后到前的查找。使用ListIterator会很方便。
ListIterator源码如下:
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-集合之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集合List接口
- Java集合源码学习(6)_List接口的实现_ArrayList_Vector
- Java单例集合下List接口的常用实现类
- java 集合框架(2)List接口
- java中的集合框架List接口
- 【java读书笔记】——Collection集合之六大接口(Collection、Set、List、Map、Iterator和Comparable)
- 一大波Java来袭(八)——Java集合之List接口
- java 集合(4) List接口 / List 常用算法
- 黑马程序员--Java学习日记之集合(collection类与list接口)及泛型概述
- java 集合 list 接口 集合的迭代 超级for循环 扩容 链表 map Hash
- java 集合类之list接口
- JAVA 常用集合接口List、Set、Map总结
- Java集合源码学习(8)_List接口的实现_CopyOnWriteArrayList
- 【慕课笔记】第四章 JAVA中的集合框架(上) 第2节 Collection接口&List接口简介
- 01-java基础加强,eclipse工具使用,快捷键,断点调试,单元测试,jdk5新特性,增强for循环,集合map,list,可变参数,枚举,反射,泛型
- Java集合源码学习(5)_List接口的基础实现AbstractList
- 13. Java类集 Part 1(类集、Collection接口、List接口、Set接口、SortedSet接口、集合的输出) ----- 学习笔记
- 【java读书笔记】——Collection集合之六大接口(Collection、Set、List、Map、Iterator和Comparable)
- 【java读书笔记】——Collection集合之六大接口(Collection、Set、List、Map、Iterator和Comparable)
- 【java随记】——Collection集合之六大接口(Collection、Set、List、Map、Iterator和Comparable)