JAVA Series -> Iterator iterator

Iterator interface

Program development, often need to traverse all elements in the collection.

In response to this demand, JDK specializes in providing an interface java.util.Iterator.

Iterator interface is a set of Java, but it is the Collection, Map interfaces are different, Map Collection interfaces and interface is mainly used to store elements, mainly for Iterator iterate (ie, traverse) elements in the Collection, so Iterator the object is also known as iterators.

Want to iterate Collection set, then the iterator will obtain the complete set of iterations, following brief acquired iterator methods:
public the Iterator Iterator (): Get set corresponding iterator to iterate over the elements of the collection.

Here are some concepts iterations:
** iteration: ** Collection that is common way to obtain a collection of elements. Before taking the element must first set has no elements is determined, if there is, to put the element taken out, continue to determine if there would no longer taken up. It has all the elements in the collection all out. This embodiment taken jargon called iteration.

Iterator interface common method as follows:
public E Next (): returns the next iteration element.
public boolean hasNext (): if the iteration has more elements, it returns true.

Example:
Here Insert Picture Description
Ps of: during collection element removed, if the set has no elements, and continue to use the iterator's next method, error java.util.NoSuchElementException no elements will occur.

The principle iterator

In the case before we have completed the entire process Iterator to traverse the collection. When traversing a collection, Iterator first by calling set () method iterator object, and then use hashNext () method determines whether there is a next element in the set, if present, is invoked next () method of the element removed, otherwise indicating that it has reached the end of the collection, stop through the elements.

Iterator iterator object in the loop through the collection, the internal use of pointers way to track the elements in the collection , in order to allow beginners to better understand how the iterator, followed by demonstration Iterator object elements by a legend:
Here Insert Picture Description
iterator before calling the next method, the iterator's index, before the first element, does not point to any element, when the first call to the next method iterator, the iterator's index will move back one, pointing to the first elements and returns that element, when the call to the next method again, the iterator's index will point to the second element and the element returns, and so on, until hasNext method returns false, indicating that reached the end of the collection, termination of traversing elements.

Enhanced for loop

Enhanced for loop (also called for each cycle) is JDK1.5 came out of the loop for a high-level, designed to loop through the array and collections. Its interior is actually a principle Iterator iterator, so the process of traversal, can not add or delete elements of the collection operation .

format

for(元素的数据类型 变量 : Collection集合or数组){ 
  //写操作代码 
}

It is used to traverse the Collection and arrays. Usually carried out only through the elements, additions and deletions do not operate on a set of elements in the process of traversal.

Examples

Collection<String> coll = new ArrayList<String>(); 
coll.add("嘿嘿"); 
coll.add("哈哈"); 
coll.add("呵呵"); 
//使用增强for遍历 
for(String s :coll){  //接收变量s代表 代表被遍历到的集合元素 
  System.out.println(s); 
}
Published 43 original articles · won praise 28 · views 2903

Guess you like

Origin blog.csdn.net/qq_16397653/article/details/103944926