Java遍历List有三种方式
| public static void main(String[] args) { |
| List<String> list = new ArrayList<>(); |
| |
| |
| for (int i = 0; i < list.size(); i++) { |
| System.out.println(list.get(i) + ", "); |
| } |
| |
| |
| for (String s : list) { |
| System.out.println(s + ", "); |
| } |
| |
| |
| Iterator<String> it = list.iterator(); |
| while (it.hasNext()) { |
| System.out.println(it.next() + ", "); |
| } |
| } |
Iterator源码
| public interface Iterator<E> { |
| |
| boolean hasNext(); |
| |
| |
| E next(); |
| |
| |
| default void remove() { |
| throw new UnsupportedOperationException("remove"); |
| } |
| |
| default void forEach(Consumer<? super T> action) { |
| Objects.requireNonNull(action); |
| for (T t : this) { |
| action.accept(t); |
| } |
| } |
| } |
| |
- 它对 Iterable 的每个元素执行给定操作,具体指定的操作需要自己写Consumer接口通过accept方法回调出来。
| List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3)); |
| list.forEach(new Consumer<Integer>() { |
| @Override |
| public void accept(Integer integer) { |
| System.out.println(integer); |
| } |
| }); |
| |
- List 的关系图谱中并没有直接使用 Iterator,而是使用 Iterable 做了过渡
| public interface Iterable<T> { |
| |
| Iterator<T> iterator(); |
| |
| default void forEach(Consumer<? super T> action) { |
| Objects.requireNonNull(action); |
| for (T t : this) { |
| action.accept(t); |
| } |
| } |
| |
| default Spliterator<T> spliterator() { |
| return Spliterators.spliteratorUnknownSize(iterator(), 0); |
| } |
| } |
ArrayList重写了Iterable的iterator方法
| public Iterator<E> iterator() { |
| return new Itr(); |
| } |
| private class Itr implements Iterator<E> { |
| |
| int cursor; |
| |
| int lastRet = -1; |
| |
| int expectedModCount = modCount; |
| |
| Itr() {} |
| |
| |
| public boolean hasNext() { |
| return cursor != size; |
| } |
| |
| @SuppressWarnings("unchecked") |
| |
| public E next() { |
| checkForComodification(); |
| |
| int i = cursor; |
| if (i >= size) |
| throw new NoSuchElementException(); |
| |
| Object[] elementData = ArrayList.this.elementData; |
| if (i >= elementData.length) |
| throw new ConcurrentModificationException(); |
| |
| cursor = i + 1; |
| |
| return (E) elementData[lastRet = i]; |
| } |
| |
| |
| public void remove() { |
| |
| if (lastRet < 0) |
| throw new IllegalStateException(); |
| |
| checkForComodification(); |
| |
| try { |
| |
| ArrayList.this.remove(lastRet); |
| |
| cursor = lastRet; |
| |
| lastRet = -1; |
| |
| expectedModCount = modCount; |
| } catch (IndexOutOfBoundsException ex) { |
| throw new ConcurrentModificationException(); |
| } |
| } |
| |
| @Override |
| @SuppressWarnings("unchecked") |
| public void forEachRemaining(Consumer<? super E> consumer) { |
| Objects.requireNonNull(consumer); |
| final int size = ArrayList.this.size; |
| int i = cursor; |
| if (i >= size) { |
| return; |
| } |
| final Object[] elementData = ArrayList.this.elementData; |
| if (i >= elementData.length) { |
| throw new ConcurrentModificationException(); |
| } |
| while (i != size && modCount == expectedModCount) { |
| consumer.accept((E) elementData[i++]); |
| } |
| |
| cursor = i; |
| lastRet = i - 1; |
| checkForComodification(); |
| } |
| |
| final void checkForComodification() { |
| if (modCount != expectedModCount) |
| throw new ConcurrentModificationException(); |
| } |
| } |
- Map 就没办法直接使用 for-each,因为 Map 没有实现 Iterable 接口,只有通过
map.entrySet()
、map.keySet()
、map.values()
这种返回一个 Collection 的方式才能 使用 for-each。
LinkedList的父类 AbstractSequentialList重写 Iterable 接口的 iterator 方法
- LinkedList 并没有直接重写 Iterable 接口的 iterator 方法,而是由它的父类 AbstractSequentialList 来完成
| public Iterator<E> iterator() { |
| return listIterator(); |
| } |
- LinkedList重写了listIterator方法
| public ListIterator<E> listIterator(int index) { |
| checkPositionIndex(index); |
| return new ListItr(index); |
| } |
| |
| |
| public interface ListIterator<E> extends Iterator<E> { |
| |
| boolean hasNext(); |
| |
| E next(); |
| |
| boolean hasPrevious(); |
| |
| E previous(); |
| |
| int nextIndex(); |
| |
| int previousIndex(); |
| |
| void remove(); |
| |
| void set(E e); |
| |
| void add(E e); |
| } |
| private class DescendingIterator implements Iterator<E> { |
| |
| private final ListItr itr = new ListItr(size()); |
| |
| |
| public boolean hasNext() { |
| return itr.hasPrevious(); |
| } |
| |
| |
| public E next() { |
| return itr.previous(); |
| } |
| |
| |
| public void remove() { |
| itr.remove(); |
| } |
| } |
本文作者:n1ce2cv
本文链接:https://www.cnblogs.com/sprinining/p/18300962
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步