Iterator object and Iterator generator object

Source: Internet
Author: User
Tags iterable

Iterator object and Iterator generator object

Directory:

Use of Iterator

   

"Iteration string set

Iteration object set

Graph and Principle Analysis of the iterator

Java iterator source code

Iterator usage:

   

"Iteration string set

        
Import java. util. arrayList; import java. util. collection; import java. util. iterator;/*** set iteration ** the Iterator iterator () set's dedicated Iterator ** E next () gets the current element of the pointer, the pointer moves to the next element * NoSuchElementException without such an element, because you have found the last * boolean hasNext (). If there are still elements that can be iterated, true is returned. */Public class IteratorDemo {public static void main (String [] args) {// create a Collection c = new ArrayList (); // Add the element c to the Collection. add ("hello"); c. add ("world"); c. add ("java"); // Iterator iterator () Iterator, set the dedicated iteration method Iterator it = c. iterator (); // Iterator is an interface, and the iterator () method returns an implementation class. Here it is a multi-state System. out. println (it. next (); System. out. println (it. next (); System. out. println (it. next (); // System. out. println (it. next (); // String s = null; // while (it. hasNext () {// s = (String) it. next (); // System. out. println (s );//}}}
  

Iteration object set

      
Import java. util. arrayList; import java. util. collection; import java. util. iterator;/***** exercise: Use a set to store five student objects, traverse students, and use an Iterator to traverse students. ** Question 1: Can a while loop be used? * Question 2: Do not use the it. next () method twice, because each use accesses an object. */Public class IteratorTest {public static void main (String [] args) {// create Collection object Collection c = new ArrayList (); // create Student s1 = new Student ("Lin Qing", 26); Student s2 = new Student ("Zhou runfa", 45 ); student s3 = new Student ("Huang jianxiang", 25); Student s4 = new Student ("Xie Yunfeng", 30); Student s5 = new Student ("Faye Wong", 30 ); // Add the student object to the set. add (s1); c. add (s2); c. add (s3); c. add (s4); c. add (s5); // get the set Iterator it = c. iterator (); // use the iterator to traverse the Student set Student s = null; while (it. hasNext () {s = (Student) it. next (); System. out. println (s. getName () + "------" + s. getAge (); // NoSuchElementException do not use it multiple times. next () method // System. out. println (Student) it. next ()). getName () + "-----" + (Student) it. next ()). getAge ();} // for Loop rewriting/* Student s = null; for (Iterator it = c. iterator (); it. hasNext ();) {s = (Student) it. next (); System. out. println (s. getName () + "------" + s. getAge ());}*/}}

 

  

//bean

      
public class Student {    private String name;    private int age;            public Student() {        super();    }    public Student(String name, int age) {        super();        this.name = name;        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    }

 

Graph and Principle Analysis of the iterator

Java iterator source code

      
Public interface Inteator {boolean hasNext (); 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 {// The implementation class contains the Iterator interface implementation public Iterator iterator () {return new Itr ();} private class Itr implements Iterator {// is an internal class and is a private public boolean hasNext () not known to outsiders () {} public Object next () {}} Collection c = new ArrayList (); 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 );}

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.