Understanding the two-way iteration of java-9.6 ListIterator from the beginning

Source: Internet
Author: User

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.

-----------------------------------

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.