Iteration output (Iterator, ListIterator, Enumeration, foreach)

table of Contents


Iterator

The data only once a positive cycle of output.
Here Insert Picture Description

Common methods:

method Explanation
public boolean hasNext() When it is determined whether the data is a null
default void remove() Delete data (difficult to use)
public E next() Get the current data

Example:

List<String> list = new ArrayList<>();
        list.add("1");
        list.add("3");
        list.add("5");
        list.add("7");
        list.add("9");
        list.add("2");
        list.add("4");
        list.add("6");
        list.add("8");
        list.add("10");
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {	//判断下一个数据是否为空
            String value = iterator.next();		//获取当前数据
            if ("3".equals(value)) {
                iterator.remove();	//删除数据,不会影响节点
            } else {
                System.out.println(value);
            }
        }

result:

1
5
7
9
2
4
6
8
10

Among the List, Set, Collection, List has a get () method, you can get the data according to the index.
List of iterators output get () method: using the get () method is required every time the index matching the output data, time complexity of (n + 1) n, performance is not strong .

ListIterator (bidirectional iterator output)

Data can be sequentially output, the output data may be reverse.

ListIterator in the presence of a reverse operation of the internal pointer desired output, a data pointer reaches must then, in the output can be reversed, so want to reverse the output, the output must be positive.
Here Insert Picture Description

Common methods:

method Explanation
public boolean hasPrevious() A determination on whether the data is empty
public E previous(); retrieve data

Example:

List<String> list = new ArrayList<>();
        list.add("a");
        list.add("c");
        list.add("b");
        list.add("m");
        list.add("a");
        list.add("d");
        list.add("g");
        list.add("j");
        list.add("l");
        list.add("q");
        ListIterator<String> listIterator = list.listIterator();
        System.out.print("正向输出:");
        while (listIterator.hasNext()) {
            System.out.print(listIterator.next() + "、");
        }
        System.out.print("\n反向输出:");
        while (listIterator.hasPrevious()) {
            System.out.print(listIterator.previous() + "、");
        }

result:

正向输出:a、c、b、m、a、d、g、j、l、q、
反向输出:q、l、j、g、d、a、m、b、c、a、

Enumeration

Early iterations output is now only Vector can be used.
Here Insert Picture Description
Common methods:

method Explanation
public boolean hasMoreElements() When it is determined whether the data is a null
public E nextElement() Get the current data

Example:

 Vector<String> vector = new Vector<>();
        vector.add("a");
        vector.add("b");
        vector.add("c");
        vector.add("d");
        vector.add("e");
        vector.add("f");
        Enumeration<String> enumeration = vector.elements();
        while(enumeration.hasMoreElements()){
            System.out.println(enumeration.nextElement());
        }

result:

a
b
c
d
e
f

foreach

foreach loop output, when the class object to a custom output, must implement the Iterable interface, returns an iterator iterator object instance of the overridden method in the interface must be implemented within the defined Iterator inner class.

Here Insert Picture Description

Example: custom class output

public class Demo01 {
    public static void main(String[] args) {
        Ball ball = new Ball();
        for (String b:ball) {
            System.out.print(b+"、");
        }
    }
}
class Ball implements Iterable<String>{
    private String[] brand = new String[]{"Hello","World"};
    private int footer;

    @Override
    public Iterator<String> iterator() {
        return new BallIter();
    }

    private class BallIter implements Iterator<String>{

        @Override
        public boolean hasNext() {
            //表示数组存在数据
            return Ball.this.footer < Ball.this.brand.length;
        }

        @Override
        public String next() {
            //返回数据
            return brand[Ball.this.footer++];
        }
    }
}

result:

Hello、World、

Example: Custom output list foreach

public class Demo02 {
    public static void main(String[] args) {
        ILink<String> link = new LinkImpl<>();
        link.add("1");
        link.add("2");
        for (String str:link) {
            System.out.println(str+"、");
        }
    }
}
interface ILink<T> extends Iterable<T>{
    /**
     *链表数据增加
     * @param data 数据
     */
    public void add(T data);
}
class LinkImpl<T> implements ILink<T>{
    private Node root;
    private Node<T> last;
    private Node currentNode;

    @Override
    public Iterator<T> iterator() {
        this.currentNode = this.root;
        return new LinkIter<>();
    }

    private static class Node<T>{
        private T data;
        private Node next;

        public Node() {

        }

        public Node(T data) {
            this.data = data;
        }
    }
    @Override
    public void add(T data) {
        Node<T> node = new Node<>(data);
        if(this.root == null){
            this.root = node;
        }else{
            this.last.next = node;
        }
        this.last = node;
    }

    private class LinkIter<T> implements Iterator<T>{

        @Override
        public boolean hasNext() {
            return LinkImpl.this.currentNode!=null;
        }

        @Override
        public T next() {
            T data = (T) LinkImpl.this.currentNode.data;
            LinkImpl.this.currentNode = LinkImpl.this.currentNode.next;
            return data;
        }
    }
}

result:

12
Published 61 original articles · won praise 0 · Views 2181

Guess you like

Origin blog.csdn.net/sabstarb/article/details/104602390