[Java class set] _ foreach and enumeration interface notes (instance test)

Source: Internet
Author: User

[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 !!! ,

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.