Java Collection Collection Detailed 3

Source: Internet
Author: User
Tags iterable

 Packagecn.itcast_03; Public  class Student {    //Member variables    PrivateString name;Private intAge//Construction method     Public Student() {Super(); } Public Student(String name,intAge) {Super(); This. name = name; This. Age = Age; }//Member Method    //GetXxx ()/setxxx ()     PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; } Public int Getage() {returnAge } Public void Setage(intAge) { This. Age = Age; }@Override     PublicStringtoString() {return "Student [name="+ name +", age="+ Age +"]"; }}
 Packagecn.itcast_03;ImportJava.util.ArrayList;ImportJava.util.Collection;ImportJava.util.Iterator;/* * Exercise: Store 5 student objects in a collection and traverse the student objects. Iterate with iterators. * * Note: * A: Your class name should not be the same as the class name in the API we are learning to use. * B: When copying the code, it is easy to import the package that the class is in, which is prone to problems that cannot be understood. */ Public  class iteratortest {     Public Static void Main(string[] args) {//Create collection ObjectCollection C =NewArrayList ();//Create student ObjectsStudent S1 =NewStudent ("Brigitte", -); Student s2 =NewStudent ("The Wind", -); Student s3 =NewStudent ("Make the fox Rush", -); Student S4 =NewStudent ("Wu Xin", -); Student S5 =NewStudent ("Lynn Song", A);//Add students to the collectionC.add (S1);        C.add (S2);        C.add (S3);        C.add (S4); C.add (S5);//TraversalIterator it = C.iterator (); while(It.hasnext ()) {//System.out.println (It.next ());Student s = (Student) it.next (); System.out.println (S.getname () +"---"+ s.getage ()); }    }}
Package CN. Itcast_03;Import Java. Util. ArrayList;Import Java. Util. Collection;Import Java. Util. Iterator;/* * Question 1: Can I use the while loop to write this program, can I have a for loop? * Issue 2: Do not use the It.next () method multiple times because each use is to access an object. */public class IteratorTest2 {public static void main (string[] args) {//Create collection Object Collection c = new Arrayl IST ();Create student object Student S1 = new Student ("Brigitte", -);Student s2 = new Student ("The Wind", -);Student s3 = new Student ("Make the fox Rush", -);Student S4 = new Student ("Wu Xin", -);Student S5 = new Student ("Lynn Song", A);Add students to the collection C. Add(S1);C. Add(S2);C. Add(S3);C. Add(S4);C. Add(S5);Traverse Iterator it = C. Iterator();while (it. Hasnext()) {Student s = (Student) it. Next();System. out. println(s. GetName() +"---"T. Getage());Nosuchelementexception don't use it multiple times. Next() method//System. out. println((Student) it. Next()). GetName() +"---"+ ((Student) it. Next()). Getage());}//System. out. println("----------------------------------");For loop overwrite//for (Iterator it = C. Iterator(); It.hasnext ();) {Student s = (Student) it. Next();System. out. println(s. GetName() +"---"+ S. Getage());// }    }}
Package CN. Itcast_03;Import Java. Util. ArrayList;Import Java. Util. Collection;Import Java. Util. Iterator;/* * Iterator Iterator (): iterator, the private traversal method of the collection * Object next (): Gets the element and moves to the next position. * Nosuchelementexception: No such element, because you have found the last. * Boolean hasnext (): Returns True if there are still elements that can iterate. (*/public class Iteratordemo {public static void main (string[] args) {//Create collection Object Collection c = new Arrayli St ();Create and add element//String s ="Hello";C. Add(s);C. Add("Hello");C. Add("World");C. Add("Java");Iterator Iterator (): iterator, private traversal of the collection Iterator it = C. Iterator();//The actual return is definitely a subclass object, here is the polymorphicObject obj = it. Next();System. out. println(obj);System. out. println(It. Next());System. out. println(It. Next());System. out. println(It. Next());System. out. println(It. Next());The last one should not be written, so, we should be in every acquisition before, if there is a judgment on it//to determine whether there is the next element, there is to get, not to ignore it//if (it. Hasnext()) {//System. out. println(It. Next());}//if (it. Hasnext()) {//System. out. println(It. Next());}//if (it. Hasnext()) {//System. out. println(It. Next());}//if (it. Hasnext()) {//System. out. println(It. Next());}//if (it. Hasnext()) {//System. out. println(It. Next());}//The final version of code while (it. Hasnext()) {//System. out. println(It. Next());string s = (string) it. Next();System. out. println(s);}    }}

The source of the iterator is as follows:

 Public  interface inteator {    BooleanHasnext (); Object next (); } Public  interface iterable {Iterator Iterator ();} Public  interface Collection extends iterable {Iterator Iterator ();} Public  interface List extends Collection {Iterator Iterator ();} Public  class ArrayList implements List {     PublicIteratoriterator() {return NewItr (); }Private  class Itr implements Iterator {         Public Boolean Hasnext() {} PublicObjectNext() {}}}collection c =NewArrayList (); C.add ("Hello"); C.add ("World"); C.add ("Java"); Iterator it = C.iterator ();//new Itr (); while(It.hasnext ())    {String s = (string) it.next (); System.out.println (s);}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Collection Collection Detailed 3

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.