1. Iterator iteration output (95%)
Iterator is the primary interface for the collection output, the interface is defined as follows:
Public Interface Iterator<e> { publicboolean//public E Next () ; // }
Implementation code:
1 PackageCn.demo;2 3 Importjava.util.ArrayList;4 ImportJava.util.Iterator;5 Importjava.util.List;6 7 Public classTest {8 Public Static voidMain (string[] args)throwsException {9list<string> all =NewArraylist<string>();TenAll.add ("Java"); OneAll.add ("JSP"); AAll.add ("Oracle"); -Iterator<string> iter =all.iterator (); - while(Iter.hasnext ()) { theString str =Iter.next (); - System.out.println (str); - } - } +}
Results:
Java
Jsp
Oracle
2. Old enumeration output: enumeration (4.96%)
This interface is defined as follows:
Public Interface Enumeration<e> { publicboolean// Determine if there is a next element Public // get current element }
The collection interface does not define a method for acquiring a enumeration interface object, and the instantiated object of this interface relies on the vector class.
There are methods for defining this class: public enumeration<e> elements ();
The code is as follows:
1 PackageCn.demo;2 3 Importjava.util.Enumeration;4 ImportJava.util.Vector;5 6 Public classTest {7 Public Static voidMain (string[] args)throwsException {8vector<string> all =NewVector<string>();9All.add ("Java");TenAll.add ("JSP"); OneAll.add ("Oracle"); AEnumeration<string> ENU =all.elements (); - while(Enu.hasmoreelements ()) { - System.out.println (Enu.nextelement ()); the } - } -}
Results:
Java
Jsp
Oracle
Summary: 1, iterator belongs to set the most standard practice, two methods: Hashnext (), Next ();
2, enumeration belongs to the vector legacy method, two methods: hasMoreElements (), Nextelement ().
Set output Interface-iterator iteration output-old enumeration output: enumeration