[Java class set] _ foreach and enumeration interface notes
Objectives of this chapter:
Understanding foreach's support for set output
Understanding the enumeration interface and usage requirements
3. Details
Foreach can not only output arrays, but also output class sets.
import java.util.ArrayList;import java.util.List;public class ForeachDemo01{ public static void main(String[] args){ List<String> all = new ArrayList<String>(); all.add("hello"); all.add("_"); all.add("world!!!"); for(String str:all){ System.out.print(str+"、"); } }}
Output: Hello, _, world !!! ,
In fact, iterator is a new output interface. If you want to output it when Java first came out, use the eunmeration interface to complete the output.
However, I still use enumeration output in some places in Java because of the historical development issues.
Note that when enumeration is used for output, the vector class is usually directly operated.
Import Java. util. vector; import Java. util. enumeration; public class enumerationdemo01 {public static void main (string [] ARGs) {vector <string> All = new vector <string> (); All. add ("hello"); All. add ("_"); All. add ("world !!! "); Enumeration <string> enu = All. elements (); While (enu. hasmoreelements () {system. out. print (enu. nextelement () + ","); // output element: Next ()}}}
Output: Hello, _, world !!! ,