Iterator java in meaning

 May iteration is Java one kind of common for all collections under the framework of the collection, that is, all elements in the collection to traverse it again. Iterative process relies on an iterator object, then what is the iterator it?

   Iterator ( the Iterator ) mode, also called vernier mode, its meaning is to provide a method for accessing a container object in each element, without exposing the details of the object.

   Note: the Java collections of collections framework, sometimes also called containers.

By definition, the iterator is born for the container, which in essence is a kind of traversal algorithm. Because achieve vastly different containers, often impossible to know how to traverse the elements of a collection of objects. Java provides us with an iterative interface, Java is all collections lost to iterate.

Simply put, the iterator is an interface Iterator , a class that implements this interface can be called iteration classes most of the time refers to the java.util collection classes in the package. Sample code is as follows:

 

[java]  view plain  copy
  1. public class  Test3 {   
  2.   
  3.     public static void main(String[] args) {  
  4.         List<String>list=new ArrayList<>();  
  5.         list.add("a");  
  6.         list.add("b");  
  7.         Iterator <String> = list.iterator (); // () method to obtain the iterator iterator set by set  
  8.         // call the iterator hasNext method, determine whether there is the next element  
  9.         while (it.hasNext()) {  
  10.             // The index moved an iterator, and get the current position of the element values  
  11.             System.out.println(it.next());    
  12.         }     
  13.     }  
  14. }  

 

First, we create a List collection of objects, and placed the two string objects, then () iterator method to get an iterator. iterator () method of the Iterable , interfaces specified ArrayList provides a specific implementation of this method, the iterator Iteartor interface, there are the following . 3 methods:

1.hasNext ()  whether the English method to determine a set of objects as well as the next element, if you have the last element returns false

2.next ()  the iterator to point to the next position, and the method returns a reference to the next element

3.remove ()   from the point iterator Collection remove the last element in the iteration is returned, the operation using the relatively small.

Note: From Java5.0 start, iterator can be foreach loop replaced, but foreach nature cycle also use Iterator to traverse.

to sum up:

 

Iterator, provides a way to access the various elements of a collection of objects, while the internal details do not need to expose the object. java by providing Iterator and Iterable to implement two interfaces may be iterative set of classes, the main usage iterator is: first with the hasNext () as a loop condition, then the Next () method of each element obtained, and finally make relevant operating.

Guess you like

Origin www.cnblogs.com/newcityboy/p/11203764.html