Java-9.5 Iterator

Source: Internet
Author: User

Java-9.5 Iterator

In this section, we will discuss some iterators Iterator.

1. Concept

Iterator (cursor) is a software design pattern for programming. It can be accessed over containers (such as linked lists or arrays, the designer does not need to care about the container content.

 

package com.ray.ch09;import java.util.ArrayList;import java.util.Collection;import java.util.HashSet;import java.util.Iterator;public class Test {public static void Traversal(Collection
 
   collection) {Iterator
  
    it = collection.iterator();while (it.hasNext()) {int rtn = it.next();System.out.println(rtn);}}public static void main(String[] args) {ArrayList
   
     list = new ArrayList
    
     ();for (int i = 0; i < 10; i++) {list.add(i);}HashSet
     
       set = new HashSet
      
       ();for (int i = 0; i < 10; i++) {set.add(i);}System.out.println(---------list---------);Traversal(list);System.out.println(---------set---------);Traversal(set);}}
      
     
    
   
  
 

From the code above, we can see that using the iterator, whether it is list or set, can reuse the Traversal method, which improves code reusability.

 

2. Notes

(1) We only need to receive containers, that is, we can operate on each object.

 

package com.ray.ch09;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;public class Test {public static void Traversal(Collection
 
   collection) {Iterator
  
    it = collection.iterator();while (it.hasNext()) {Person person = it.next();System.out.println(person.getId());}}public static void main(String[] args) {ArrayList
   
     list = new ArrayList
    
     ();for (int i = 0; i < 10; i++) {Person person = new Person();person.setId(i);list.add(person);}Traversal(list);}}class Person {private int id = 0;public int getId() {return id;}public void setId(int id) {this.id = id;}}
    
   
  
 

From the code above, we can convert the iterator type to the Person type, and then hold the reference of the Person object. Now we can operate on the person object.

 

 

(2) Use of the remove () method

 

Package com. ray. ch09; import java. util. ArrayList; import java. util. Iterator; public class Test {public static void main (String [] args) {ArrayList
 
  
List = new ArrayList
  
   
(); For (int I = 0; I <10; I ++) {list. add (I);} Iterator
   
    
Iterator = list. iterator (); iterator. next (); // if we comment this sentence, an exception iterator. remove () ;}} will be thrown during runtime ();}}
   
  
 

When we use the remove method, we must first use the next method so that the iterator already points to a new object.

 

 

Summary: This chapter briefly discusses the concept of the iterator and the corresponding points of attention.

 

 

 

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.