java中Iterable接口的使用,实现一个单链表的迭代器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ASJBFJSB/article/details/83957633

单链表实现:

public class MyLinkedList <T>{
    private static class Entry<E>{
        private E value;
        private Entry next;

        Entry(E e){this.value = e;this.next = null;}
        Entry(E e,Entry next){this.value = e;this.next = next;}
        Entry(){}

        public E getValue(){return value;}
        public void setValue(E value) {this.value = value;}
        public Entry getNext() { return next;}
        public void setNext(Entry next) { this.next = next;}
    }

    private Entry<T> head;
    private int length;

    MyLinkedList(){
        head = new Entry<T>();
        length = 0;
    }

    public void push(T e){
        insertTail(e);
    }
    public void pop(){
        if(!isEmpty()){
            deleteTail();
        }
        else{
            System.out.print("栈空,不能进行删除!");
        }
    }
    public T peek(){
        if(!isEmpty()){
            return getTailValue();
        }
        else {
            System.out.print("栈空,没有栈顶元素");
            return null;
        }
    }
    public boolean isEmpty(){return getLength()==0;}
    public int search(T e){
        int index = searchIndex(e);
        if(index>=0){
            return getLength()-index;
        }
        else{
            return -1;
        }
    }
    public int searchIndex(T e){
        assert(head!=null);
        Entry p = head.next;
        int i = 0;
        while(p!=null){
            if(p.value.equals(e)){
                return i;
            }
            i++;
            p = p.next;
        }
        return -1;
    }

    public void insert(int pos,T e){
            if(pos < 1 || pos >getLength()+1){
                System.out.print("插入位置不正确!");
            }
            int i = 0;
            assert(head!=null);
            Entry p = head;
            while(i<pos-1 && p!=null){
                i++;
                p = p.next;
            }
            Entry pNew = new Entry(e);
            pNew.next = p.next;
            p.next = pNew;
            length++;
    }
    public void insertHead(T e){insert(1,e);}
    public void insertTail(T e){insert(getLength()+1,e);}
    public void delete(int pos){
        if(pos<1 || pos>getLength()){
            System.out.print("删除位置不正确!");
        }
        assert(head!=null);
        int i=0;
        Entry p = head;
        while(i<pos-1 && p!=null){
            i++;p = p.next;
        }
        Entry q = p.next;
        q.value = null;
        p.next = q.next;
        length--;
    }
    public void deleteHead(){delete(1);}
    public void deleteTail(){delete(getLength());}
    public void show(){
        assert(head!=null);
        Entry p = head.next;
        while(p!=null){
            System.out.print(p.value+" ");
            p = p.next;
        }
        System.out.println();
    }
    public int getLength(){return length;}
    public T getTailValue(){
        assert(head!=null);
        Entry<T> p = head;
        while(p.next!=null){
            p = p.next;
        }
        return p.getValue();
    }
}

Iterable接口
在这里插入图片描述
Iterable接口中需要实现的抽象方法iterator()的返回值会返回一个迭代器对象,这个迭代器对象可以作为一个工具来遍历集合类中的对象。此外,迭代器更是设计模式,如对图的遍历可以实现一个图迭代器,简化代码,将遍历的思想抽象出来。

自己实现一个可以遍历上述单链表的迭代器,这个迭代器需要实现Iterator接口中的方法。主要包括以下三个方法:
在这里插入图片描述
(1)是否存在下一个对象元素
(2)返回下一个对象元素
(3)删除集合中的当前迭代器指向的对象元素

public class MyLinkedList <T> implements Iterable<T>{
    public Iterator iterator(){
        return new MyIterator();
    }

	public class MyIterator implements Iterator<T> {
        Entry<T> data;

        MyIterator(){
            data = head.next;
        }
        @Override
        public boolean hasNext() {
            return data != null;
        }

        @Override
        public T next() {
            Entry<T> last = data;
            data = data.next;
            return last.getValue();
        }

        @Override
        public void remove() {
            MyLinkedList1.this.delete(searchIndex(data.getValue()));
        }
    }

测试迭代器:

public class Main {
    public static void main(String[] args){
        MyLinkedList1<Integer> list = new MyLinkedList1<>();
        for(int i=0;i<10;++i){
            list.insertTail(i);
        }
        list.show();
        Iterator it = list.iterator();
        while(it.hasNext()){
            System.out.print(it.next()+" ");
        }
    }
}

测试结果:
在这里插入图片描述
可以看出通过迭代器循环遍历集合中的对象元素和show()方法的功能是相同的,但是迭代器为遍历集合对象元素提供了一种统一的方法,此外也可以使用迭代器做更多的事情。

猜你喜欢

转载自blog.csdn.net/ASJBFJSB/article/details/83957633