2.1 the Iterator interface
In program development, it is often necessary to iterate over all the elements in a collection. For this purpose, the JDK provides an interface called java.util.iterator.
The Iterator interface is also a member of the Java Collection. Unlike the Collection and Map interfaces, which store elements, the Iterator interface iterates through the elements of a Collection. Hence Iterator objects are also called iterators.
To iterate over the Collection, we need to get the iterator to complete the iteration. Here’s how to get the iterator:
public Iterator iterator()
: Gets the iterator corresponding to the collection, which is used to traverse the elements in the collection. ,
Concept of iteration:
Iteration: A generic way to get elements of a Collection. Before you take an element, you have to check if there is an element in the set, if there is, you take that element out, you keep checking, if there is, you take it out again. I keep pulling all the elements out of the set. The technical term for this method of extraction is iteration.
Common methods for Iterator interfaces:
public E next()
: returns the next element of the iteration.public boolean hasNext()
: Returns true if there are still elements to iterate over.
public class IteratorDemo {
public static void main(String[] args) {
// Create objects in polymorphic mode
Collection<String> coll = new ArrayList<String>();
// Add elements to the collection
coll.add("Star Men.");
coll.add("Mocking the Stars.");
coll.add("Dog People");
/ / traverse
Each collection object has its own iterator
Iterator<String> it = coll.iterator();
// Generics refer to iterating out the data type of the element
while(it.hasNext()){ // Determine if there are iteration elements
String s = it.next();// Get the iterated elementSystem.out.println(s); }}}Copy the code
Tips: : when element to set out, if there is no element in the set, still continue to use the iterator next method, Java will happen. Util. NoSuchElementException no error collection elements.
2.2 Implementation principle of iterators
When iterating through the collection, we first get the iterator object by calling the iterator() method of the collection t. Then we use hashNext() to determine whether the next element exists in the collection. If so, we call next() to pull the element out.
Iterator an Iterator that internally traces the elements of a collection as Pointers as it traverses it.
Before calling next, the Iterator’s index precedes the first element and does not refer to any element. After the first call to next, the Iterator’s index moves back one bit to refer to the first element and returns that element. When next is called again, The iterator’s index points to the second element and returns it, and so on, until the hasNext method returns false, indicating that the end of the collection has been reached, terminating the iteration of the element.
2.3 enhance the for
The enhanced for loop (also known as the for each loop) is an advanced for loop developed after JDK1.5 that iterates through groups and collections. It’s an Iterator, so you can’t add or remove elements from the collection.
Format:
for(Element data type variable: Collection Collection or array){// Write the operation code
}
Copy the code
It is used to iterate over collections and arrays. Generally, only elements are iterated; do not add or delete collection elements during the traversal.
Traversal number group:
public class NBForDemo1 {
public static void main(String[] args) {
int[] arr = {3.5.6.87};
// Use the for enhancement to iterate over the number group
for(int a : arr){// A represents each element in the arraySystem.out.println(a); }}}Copy the code
Traversal collection:
public class NBFor {
public static void main(String[] args) {
Collection<String> coll = new ArrayList<String>();
coll.add("Little River God");
coll.add(Old River God);
coll.add("Witch");
// Use enhanced for traversal
for(String s :coll){// The receiving variable s represents the collection element to be traversedSystem.out.println(s); }}}Copy the code
Tips: New for loops must have a target to be traversed. The target can only be Collection or array. The new for appears only as a traversal operation.