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