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