Understanding the two-way iteration of java-9.6 ListIterator from the beginning
In this chapter, we will briefly discuss ListIterator.
ListIterator mainly supplements the problem that Iterator can only move backward, and can move forward in ListIterator.
package com.ray.ch09;import java.util.ArrayList;import java.util.ListIterator;public class Test {public static void main(String[] args) {ArrayList
arrayList = new ArrayList
();for (int i = 0; i < 10; i++) {arrayList.add(i);}ListIterator
iterator = arrayList.listIterator();while (iterator.hasNext()) {Integer rtn = iterator.next();System.out.print(rtn);}System.out.println();while (iterator.hasPrevious()) {Integer rtn = iterator.previous();System.out.print(rtn);}}}
Output:
0123456789
9876543210
Note: Before using iterator. previous (), you must check whether iterator. hasPrevious () is true or false. Otherwise, an exception is thrown when the first element is not pushed into the iterator.
Summary: This chapter briefly describes two-way iteration of ListIterator.
This chapter is here. Thank you.
-----------------------------------