List-specific iterator-Special Functions of ListIterator, list iterator

Source: Internet
Author: User

List-specific iterator-Special Functions of ListIterator, list iterator

/**
*List iterator:
* ListIterator listIterator (): List set unique iterator
* This Iterator inherits the Iterator, so you can directly use the hasNext () and next () methods.
*Special features:
* Object previous (): gets the previous element.
* Boolean hasPrevious (): determines whether the previous element exists.
*
*Note:ListIterator can traverse the list in reverse order, but the premise is that the list can be traversed first and then reversely. Generally, it is not used.
*
*

Import java. util. arrayList; import java. util. list; import java. util. listIterator;/*** List Iterator: * ListIterator listIterator (): List Set-specific Iterator * This Iterator inherits the Iterator. Therefore, you can directly use the hasNext () and next () methods. * Special function: * Object previous (): gets the previous element * boolean hasPrevious (): determines whether the previous element exists. ** Note: ListIterator can traverse the list in reverse order, however, the premise is that a forward traversal can be performed before reverse traversal. Generally, it is not used. **/Public class ListIteratorDemo {public static void main (String [] args) {List list = new ArrayList (); list. add ("hello"); list. add ("world"); list. add ("java"); // actively traverses ListIterator liter = list. listIterator (); while (liter. hasNext () {String s = (String) liter. next (); System. out. println (s);} System. out. println ("---------------------"); // reverse traversal while (liter. hasPrevious () {String s = (String) liter. previous (); System. out. println (s );}}}

Related Article

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.