Iterator mode

Iterator mode

Used to access various elements of a collection without exposing the internal details of the collection

Iterator Interface

The method of iterator is defined uniformly

hasNext() is used to determine whether there is another element

next() is used to return the next element and make the internal counter + 1

/**
 * Iterator Interface 
 */
public interface Iterator {
    public abstract boolean hasNext();

    public abstract Object  next();
}

 

Interface for generating iterators

/**
 * There is only one method in this interface, which is used to generate an iterator and return
 * <p>
 * If a collection wants to use iterators, it should implement this interface
 */
public interface Aggregate {
    public abstract Iterator iterator();
}

  

Element class

In this case, the iterator is used to traverse each cat in the "cat group"

So before creating a cat group, define the cat category

/**
 * Cat Is an individual, a Cat instance represents a Cat
 * CatGroup A CatGroup instance represents a group of cats
 */
public class Cat {
    private int    age;
    private String name;

    public Cat(String name, int age) {
        this.age = age;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                '}';
    }
}

 

Collection class

Next, create the cat group class

If the cat group class wants to use the iterator, it needs to implement the iterator() method of the Aggregate interface first to obtain the iterator that provides the group

/**
 * Cat Is an individual, a Cat instance represents a Cat
 * CatGroup A CatGroup instance represents a group of cats
 *
 * And this class implements the iterator() method of Aggregate to get the iterator
 */
public class CatGroup implements Aggregate {

    private int last;
    private Cat[] cats;

    public CatGroup(int maxsize) {
        this.cats = new Cat[maxsize];
    }

    public Cat getByIndex(int index) {
        return cats[index];
    }

    public void append(Cat cat) {
        cats[last++] = cat;
    }

    public int getLength() {
        return last;
    }

    @Override
    public Iterator iterator() {
        return new CatGroupIterator(this);
    }
}

  

Set iterator class

Next, we introduce the iterator of cat group

hasNext() is used to determine whether there is another cat in the cat group. If the index is less than the number of cats in the cat group, there must be the next one; otherwise, there must be no next cat, indicating that all cats have been traversed

The next() method is responsible for returning "next cat" directly and adding the counter + 1

public class CatGroupIterator implements Iterator {
    private int index;
    private CatGroup catGroup;

    public CatGroupIterator(CatGroup catGroup) {
        this.catGroup = catGroup;
        this.index = 0;
    }

    @Override
    public boolean hasNext() {
        return index < catGroup.getLength();
    }

    @Override
    public Object next() {
        return catGroup.getByIndex(index++);
    }
}

  

test

public class Main {
    public static void main(String[] args) {
        // Create a cat colony
        CatGroup catGroup = new CatGroup(20);

        // Add cats to the group
        catGroup.append(new Cat("betty", 10));
        catGroup.append(new Cat("nancy", 11));
        catGroup.append(new Cat("wood", 14));
        catGroup.append(new Cat("zira", 18));

        // Get iterator traversing the cat group
        Iterator iterator = catGroup.iterator();

        // Iterate and output
        while (iterator.hasNext()) {
            Cat c = (Cat) iterator.next();
            System.out.println(c);
        }
    }
}

Tags: Java less

Posted on Thu, 19 Mar 2020 11:16:48 -0400 by rake