实现Iterable接口,优雅的使用foreach进行遍历

21 篇文章 0 订阅

首先要有一个自己的Node类,如下:

public class Node {
    private final int value;
    private Node next;

    public Node(int value) {
        this.value = value;
        this.next = null;
    }

    public int getValue() {
        return value;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}

根据这个Node类,写一个属于自己的LinkedList,同时实现Iterable接口,如下:

public class LinkedList implements Iterable<Integer> {

    Node head;
    Node tail;

    public LinkedList() {
        head = null;
        tail = null;
    }

    public void add(int value) {
        Node node = new Node(value);
        if (tail == null) {
            head = node;
        } else {
            tail.setNext(node);
        }
        tail = node;
    }

    @Override
    public Iterator<Integer> iterator() {
        return null;
    }
}

上面可以看到,实现Iterable接口需要重写iterator方法,需要我们返回一个迭代器,所以我们就使用成员内部类实现Iterator接口写一个迭代器类,如下:

private class ListIterator implements Iterator<Integer> {

        @Override
        public boolean hasNext() {
            return false;
        }

        @Override
        public Integer next() {
            return null;
        }
    }

上面可以看到我们需要重写hasNext以及next方法,我们查看源码,看看接口里面是怎么说明这两个方法的,如下:

/**
     * Returns {@code true} if the iteration has more elements.
     * (In other words, returns {@code true} if {@link #next} would
     * return an element rather than throwing an exception.)
     *
     * @return {@code true} if the iteration has more elements
     */
    boolean hasNext();

    /**
     * Returns the next element in the iteration.
     *
     * @return the next element in the iteration
     * @throws NoSuchElementException if the iteration has no more elements
     */
    E next();

源码的意思是说,如果还有元素可以迭代那么hasNext就返回true,否则返回false。next方法中是返回下一个迭代的元素,如果没有该元素则抛出NoSuchElementException异常。
所以我们就按照接口说明来实现这两个方法。

private class ListIterator implements Iterator<Integer> {

        Node node;

        public ListIterator(Node node) {
            this.node = node;
        }

        @Override
        public boolean hasNext() {
            return node != null;
        }

        @Override
        public Integer next() {
            if (node == null) {
                throw new NoSuchElementException();
            }
            int value = node.getValue();
            node = node.getNext();
            return value;
        }
    }

自写的LinkedList完整代码如下:

public class LinkedList implements Iterable<Integer> {

    Node head;
    Node tail;

    public LinkedList() {
        head = null;
        tail = null;
    }

    public void add(int value) {
        Node node = new Node(value);
        if (tail == null) {
            head = node;
        } else {
            tail.setNext(node);
        }
        tail = node;
    }

    @Override
    public Iterator<Integer> iterator() {
        return new ListIterator(head);
    }

    private class ListIterator implements Iterator<Integer> {

        Node node;

        public ListIterator(Node node) {
            this.node = node;
        }

        @Override
        public boolean hasNext() {
            return node != null;
        }

        @Override
        public Integer next() {
            if (node == null) {
                throw new NoSuchElementException();
            }
            int value = node.getValue();
            node = node.getNext();
            return value;
        }
    }
}

至此,我们写的LinkedList类就可以优雅的使用forEach进行遍历了

public static void main(String[] args) {
        LinkedList list = new LinkedList();
        for (int i = 0; i < 100; i++) {
            list.add(i);
        }

        for (Integer value : list) {
            System.out.println(value);
        }
    }
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JackieLeeee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值