Java learning note 28 (set framework II: Iterator iterator)

Iterator interface, translated into Chinese is iterator

The concept of iterator is generally explained as follows:

There are many collections in java. There are various internal storage methods and different ways to get them. Can there be a general way to get them?

This is similar to for loop traversal,

General method of obtaining: before taking out the element, judge whether there is this element in the collection, if so, take it out; continue to judge, if there is still one, continue to take it out until all are taken out, which is iteration

 

Implementation of iterator:

(you can also change the ArrayList set to another set.)

package demo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class IteratorDemo {
    public static void main(String[] args) {
        Collection<String>c1 = new ArrayList<String>();
        c1.add("abc1");
        c1.add("abc2");
        c1.add("abc3");
        c1.add("abc4");
        Iterator<String> it1 = c1.iterator();
        boolean b1 = it1.hasNext();//Is there any element that can be removed
        System.out.println(b1);//true
        //Get element
        while(it1.hasNext()){
            String s = it1.next();
            System.out.println(s);
        }
    }
}

 

Transformation in set iteration:
It is recommended to add the type, but it can also be implemented if you do not join:

package demo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CollectionDemo {
    public static void main(String[] args) {
        //Collection can store any type of object
        //In a collection, you can store data without specifying the type of data to store
        Collection c1 = new ArrayList();
        c1.add("abc");
        c1.add("def");
        Iterator it1 = c1.iterator();
        while(it1.hasNext()){
            //it.next Obtain Object Type, cast
            String s1 = (String)it1.next();
            System.out.println(s1.length());
        }
    }
}

 

Using the principle of iterator, the enhanced for loop appears after JDK 1.5:

Example of enhanced for loop traversal array:

package demo;
//Enhance for Loop, with fixed format

//Advantage: less code, easy to traverse
//Disadvantage: no index, unable to operate the elements in the container
public class ForEachDemo {
    public static void main(String[] args) {
        function1();
        function2();
    }
    
    public static void function1(){
        int[] arr = {3,2,5,4,8,9,6};
        //Ergodic output
        for(int i :arr){
            System.out.println(i);
        }
    }
    
    public static void function2(){
        //When traversing, you can call the object's methods
        String[] str1 = {"abc","def","java"};
        for(String s:str1){
            System.out.println(s);
            System.out.println(s.length());
        }
    }
}

 

Example of enhanced for loop traversal set:

package demo;

import java.util.ArrayList;

//Enhance for Loop, with fixed format
public class ForEachDemo {
    public static void main(String[] args) {
        function();
    }

    public static void function() {
        ArrayList<String> array1 = new ArrayList<String>();
        array1.add("abcd");
        array1.add("java");
        for (String s : array1) {
            System.out.println(s);
        }
    }
}

Keywords: Java JDK less

Added by alasdair12 on Sat, 02 May 2020 10:32:50 +0300