Java Basic Knowledge Hardening Collection Framework note 12:collection Collection stores strings and iterates through

Source: Internet
Author: User

1. Collection collection stores strings and iterates through

Analysis:

(1) Creating a Collection Object

(2) Creating a String Object

(3) Adding a String object to the collection

(4) Traversing the collection

2. code example:

1  Packagecn.itcast_04;2 3 Importjava.util.ArrayList;4 Importjava.util.Collection;5 ImportJava.util.Iterator;6 7 /*8 * Requirement: Store string and traverse. 9  * Ten * Analysis: One * A: Create A Collection Object  A * B: Create a String object  - * C: Add a String object to the collection  - * D: traversing the collection  the  */ -  Public classCollectiontest { -      Public Static voidMain (string[] args) { -         //To create a collection object +Collection C =NewArrayList (); -  +         //creating a String Object A         //to add a string object to the collection atC.add ("Brigitte"); -C.add ("Breeze"); -C.add ("Elina"); -C.add ("Wu Xin"); -C.add ("Lynn Song"); -  in         //iterating through the collection -         //getting an iterator object from a collection object toIterator it =c.iterator (); +         //the Hasnext () method of an Iterator object to determine if there are any elements -          while(It.hasnext ()) { the             //get an element by the next () method of an Iterator object *String s =(String) It.next (); $ System.out.println (s);Panax Notoginseng         } -     } the}

The results of the operation are as follows:

Java Basic Knowledge Hardening Collection Framework note 12:collection Collection stores strings and iterates through

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.