【Java 笔记】Iterator迭代器、Vector容器与ArrayList

jdk8 document

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

Iterator迭代器
  • 迭代器是一种模式,使数据结构的遍历行为与被遍历的对象分离,即无需关心该序列的底层结构是什么样子的。只要拿到这个对象,使用迭代器就可以遍历这个对象的内部。
  • 注意:迭代出来的元素都是原来集合元素的拷贝。
    Java集合中保存的元素实质是对象的引用,而非对象本身。
    迭代出的对象也是引用的拷贝,结果还是引用。那么如果集合中保存的元素是可变类型的,那么可以通过迭代出的元素修改原集合中的对象。
  • 那么当Iterator访问Collection集合中元素时,Collection的元素不能改变(多个线程的修改),只能通过Iteratorremove()方法删除。否则会引发ModificationException异常(异常原因简单来说是iterator源代码在检查modCount的值,这个值被ArrayList改变时抛出异常)
  • Iterator对集合类中的任何一个实现类,都可以返回这样一个Iterator对象。可以适用于任何一个类。
  • 从数据结构角度分析,for循环适合访问顺序结构,可以根据下标快速获取指定元素.而Iterator 适合访问链式结构,因为迭代器是通过next()和Pre()来定位的.可以访问没有顺序的集合.
  • 而使用 Iterator 的好处在于可以使用相同方式去遍历集合中元素,而不用考虑集合类的内部实现(只要它实现了 java.lang.Iterable 接口),如果使用 Iterator 来遍历集合中元素,一旦不再使用 List 转而使用 Set 来组织数据,那遍历元素的代码不用做任何修改,如果使用 for 来遍历,那所有遍历此集合的算法都得做相应调整,因为List有序,Set无序,结构不同,他们的访问算法也不一样.(还是说明了一点遍历和集合本身分离了)
    ————————————————
    原文链接:https://blog.csdn.net/Jae_Wang/article/details/80526216
Iterator ListIterator
E next() E next()
boolean hasNext() boolean hasNext()
default void remove() 最后一个元素 void remove() ( next()/previous()返回的最后一个元素)
default void forEachRemaining(Consumer<? super E> action) x
x boolean hasPrevious()
x int nextIndex()
x int previousIndex()
x E previous()
x void set(E e) next()/previous()返回的最后一个元素
x void add(E e)

iterator只能顺序访问,ListIterator可以倒序访问、追加、重写

ArrayList 与Vector
  1. ArrayList出现于jdk1.2,vector出现于1.0.两者底层的数据存储都使用的Object数组实现,
    因为是数组实现,所以具有 查找快(因为数组的每个元素的首地址是可以得到的,数组是0序的,所以: 被访问元素的首地址=首地址+元素类型字节数*下标 ),增删慢(因为往数组中间增删元素时,会导致后面所有元素地址的改变)的特点.

  2. 线程的安全性不同,vector是线程安全的,在vector的大多数方法都使用synchronized关键字修饰,arrayList是线程不安全的(可以通过Collections.synchronizedList()实现线程安全)

  3. 性能上的差别,由于vector的方法都有同步锁,在方法执行时需要加锁、解锁,所以在执行过程中效率会低于ArrayList,另外,性能上的差别还体现在底层的Object数组上

————————————————
原文链接:https://blog.csdn.net/qq_37113604/article/details/80836025

方法比较
ArrayList方法 Vector方法
boolean add(E e) ~
void add(int index, E element) ~
boolean addAll(Collection<? extends E> c) ~
boolean addAll(int index, Collection<? extends E> c) ~
x void addElement(E obj)
x int capacity() 当前容量
void clear() ~
Object clone() ~
boolean contains(Object o) ~
void ensureCapacity(int minCapacity) ~
x boolean containsAll(Collection<?> c)
x void copyInto(Object[] anArray)
x E elementAt(int index)
x Enumeration elements()
x boolean equals(Object o)
x E firstElement()
void forEach(Consumer<? super E> action) ~
E get(int index) ~
x int hashCode()
int indexOf(Object o) ~
x int indexOf(Object o, int index)
x void insertElementAt(E obj, int index)
boolean isEmpty() ~
Iterator< E> iterator() ~
x E lastElement()
int lastIndexOf(Object o) ~
x int lastIndexOf(Object o, int index)
ListIterator < E> listIterator() ~
ListIterator< E> listIterator(int index) ~
E remove(int index) ~
boolean remove(Object o) ~
boolean removeAll(Collection<?> c) ~
x void removeAllElements()
x boolean removeElement(Object obj)
x void removeElementAt(int index)
boolean removeIf(Predicate<? super E> filter) ~
protected void removeRange(int fromIndex, int toIndex) ~
void replaceAll(UnaryOperator< E> operator) ~
boolean retainAll(Collection<?> c) ~
E set(int index, E element) ~
x void setElementAt(E obj, int index)
x void setSize(int newSize)
int size() ~
void sort(Comparator<? super E> c) ~
List< E> subList(int fromIndex, int toIndex) ~
Object[] toArray() ~
x String toString()
发布了27 篇原创文章 · 获赞 1 · 访问量 689

猜你喜欢

转载自blog.csdn.net/SUKI547/article/details/102389741