Java Basic Knowledge Hardening Collection Framework note 18:list Collection-specific Listiterator iterators

Source: Internet
Author: User

1. Listiterator (List iterator):

Listiterator Listiterator (): A list collection-specific iterator

2. code example:

1  Packagecn.itcast_04;2 3 Importjava.util.ArrayList;4 ImportJava.util.Iterator;5 Importjava.util.List;6 ImportJava.util.ListIterator;7 8 /*9 * List iterator:Ten * Listiterator listiterator (): List collection-specific iterator One * The iterator inherits the iterator iterator, so you can use the Hasnext () and Next () methods directly.  A  *  - * Unique Features: - * Object Previous (): Gets the previous element  the * boolean hasprevious (): Determine if there are elements  -  *  - * Note: Listiterator can implement reverse traversal, but must first forward traversal, in order to reverse the traversal, so generally meaningless, not used .  -  */ +  Public classListiteratordemo { -      Public Static voidMain (string[] args) { +         //To Create a list collection Object AList List =NewArrayList (); atList.add ("Hello"); -List.add ("World"); -List.add ("Java"); -  -         //listiterator listiterator () -Listiterator lit = List.listiterator ();//sub-class object in         //While (Lit.hasnext ()) { -         //string s = (string) lit.next (); to         //System.out.println (s); +         // } -         //System.out.println ("-----------------"); the          *         //System.out.println (Lit.previous ()); $         //System.out.println (Lit.previous ());Panax Notoginseng         //System.out.println (Lit.previous ()); -         //nosuchelementexception the         //System.out.println (Lit.previous ()); +  A          while(Lit.hasprevious ()) {//no print, just start (Pointer to collection list first element address first element before one is not), so there is nothing to print out  theString s =(String) lit.previous (); + System.out.println (s); -         } $System.out.println ("-----------------"); $  -         //iterators -Iterator it =list.iterator (); the          while(It.hasnext ()) { -String s =(String) It.next ();Wuyi System.out.println (s); the         } -System.out.println ("-----------------"); Wu  -     } About}

The results are as follows:

Java Basic Knowledge Hardening Collection Framework note 18:list Collection-specific Listiterator iterators

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.