自定义实现迭代器

关于Java集合类的迭代器的概念可以看下面的博客链接

Java集合类迭代器

自定义迭代器

这里我们用链表类作为说明

首先,自定义一个链表类,并实现Iterable接口,因为要重写迭代器方法
class LinkList<T> implements Iterable<T> {
    class Entry<T> {//节点类
        private T data;
        private Entry next;

        public Entry() {
            next = null;
        }

        public Entry(T val) {
            data = val;
            next = null;
        }

        public T getData() {
            return data;
        }
    }

    private Entry head;
    private int size;

    public LinkList() {
        head = new Entry<T>();
    }

	//向链表添加元素
    public void add(T val) {
        Entry<T> entry = new Entry(val);
        Entry<T> cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = entry;
        size++;
    }
	//获取指定位置的节点
    public T get(int index) {
        if (index >= size || index <= 0) {
            throw new UnsupportedOperationException("参数错误");
        }
        Entry entry = head;
        while (index > 0) {
            index = index - 1;
            entry = entry.next;
        }
        return (T) entry.getData();
    }

    private Entry getPrev(T val) {
        Entry entry = head.next;
        Entry prev = head;
        while (entry != null) {
            if (entry.getData() == val) {
                break;
            }
            entry = entry.next;
            prev = prev.next;
        }
        return prev;
    }
	//删除值为val的节点,一次删除一个
    public void remove(T val) {
        Entry pre = getPrev(val);//获取当前节点的前驱节点
        pre.next = pre.next.next;
        size--;
    }


    public void show() {
        Entry entry = this.head.next;
        while (entry != null) {
            System.out.print(entry.data + " ");
            entry = entry.next;
        }
        System.out.println();
    }
}
第二步,在自定义的链表类中创建一个内部类用来重写迭代器中三个基本方法,该类要实现Iterator接口
    class Itr implements Iterator<T> {
        private Entry entry;
        private Entry returned;

        public Itr() {
            entry = head.next;
        }

        @Override
        public void remove() {
            T del = (T) returned.getData();
            LinkList.this.remove(del);
        }

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

        @Override
        public T next() {
            if (!hasNext()) {
				throw new NoSuchElementException();
            }
            returned = entry;
            entry = entry.next;
            return (T) returned.getData();
        }
    }
重写Iterable接口中的iterator(),使其返回我们自定义的迭代器类的一个实例对象
    @Override
    public Iterator<T> iterator() {
        return new Itr();
    }

完整代码和测试结果

class LinkList<T> implements Iterable<T> {
    class Entry<T> {
        private T data;
        private Entry next;

        public Entry() {
            next = null;
        }

        public Entry(T val) {
            data = val;
            next = null;
        }

        public T getData() {
            return data;
        }
    }

    private Entry head;
    private int size;

    public LinkList() {
        head = new Entry<T>();
    }


    public void add(T val) {
        Entry<T> entry = new Entry(val);
        Entry<T> cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = entry;
        size++;
    }

    public T get(int index) {
        if (index >= size || index <= 0) {
            throw new UnsupportedOperationException("参数错误");
        }
        Entry entry = head;
        while (index > 0) {
            index = index - 1;
            entry = entry.next;
        }
        return (T) entry.getData();
    }

    private Entry getPrev(T val) {
        Entry entry = head.next;
        Entry prev = head;
        while (entry != null) {
            if (entry.getData() == val) {
                break;
            }
            entry = entry.next;
            prev = prev.next;
        }
        return prev;
    }

    public void remove(T val) {
        Entry pre = getPrev(val);
        pre.next = pre.next.next;
        size--;
    }


    public void show() {
        Entry entry = this.head.next;
        while (entry != null) {
            System.out.print(entry.data + " ");
            entry = entry.next;
        }
        System.out.println();
    }

    @Override
    public Iterator<T> iterator() {
        return new Itr();
    }

    class Itr implements Iterator<T> {
        private Entry entry;
        private Entry returned;

        public Itr() {
            entry = head.next;
        }

        @Override
        public void remove() {
            T del = (T) returned.getData();
            LinkList.this.remove(del);
        }

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

        @Override
        public T next() {
            if (!hasNext()) {

            }
            returned = entry;
            entry = entry.next;
            return (T) returned.getData();
        }
    }
}

public class LinkedListDIY {
    public static void main(String[] args) {
        LinkList<Integer> linkList = new LinkList();
        linkList.add(12);
        linkList.add(23);
        linkList.add(555555555);
        linkList.add(555);
        linkList.add(45);
        linkList.add(null);
        linkList.show();
        Iterator<Integer> iterator = linkList.iterator();
        iterator.next();
        iterator.remove();
        while (iterator.hasNext()) {
            System.out.print(iterator.next() + " ");
        }
    }
}

结果:在这里插入图片描述
由结果可以看出,自定义实现迭代器可以进行链表的遍历、删除操作

你可能感兴趣的:(自定义实现迭代器)