JavaCookbook-8. Generic foreach and enumeration

Source: Internet
Author: User

In order to store data in a Collection class and set it to the same type. We use the generic mechanism introduced by JDK1.5. The type in angle brackets is the type of input parameters and return values. For example, ArrayList <String> data = new ArrayList <String> (); both input parameters and output return values are of the String type by default. Therefore, forced type conversion is no longer required in the program. You can use the Foreach method to conveniently access methods in a set. It is equivalent to calling the Iterator method to output information. [Java] for (Stringa: list) {<span style = "white-space: pre"> </span> System. out. println (a);} is equivalent to: [java] Iterator I = list. iterator (); while (I. hasNext () {<span style = "white-space: pre"> </span> System. out. print (I. next () + "");} routine: [java] List <String> list = new ArrayList <String> (); list. add ("Welcome"); list. add ("to"); list. add ("China! "); For (String a: list) {<span style =" white-space: pre "> </span> System. out. print (a + "");} Iterator I = list. iterator (); while (I. hasNext () {<span style = "white-space: pre"> </span> System. out. print (I. next () + "");

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.