在同一类中实现Java Iterator和Iterable?

2022-09-03 03:47:03

我正在尝试理解 Java 和接口IteratorIterable

我正在写这门课

class MyClass implements Iterable<String> {
    public String[] a = null;
    public MyClass(String[] arr) {
        a = arr;    
    }

    public MyClassIterator iterator() {
        return new MyClassIterator(this);
    }

    public class MyClassIterator implements Iterator<String> {
        private MyClass myclass = null;
        private int count = 0;
        public MyClassIterator(MyClass m) {
            myclass = m;    
        }

        public boolean hasNext() {
            return count < myclass.a.length;
        }
        public String next() {
            int t = count;
            count++;
            return myclass.a[t];
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    }   
}

它似乎正在起作用。

我应该有:

Myclass implements Iterable<Stirng>, Iterator<String> {

}

或者我应该把外面作为MyClassIteratorMyClass

class MyClass implements Iterable<String> {
    public String[] a = null;
    public MyClass(String[] arr) {
        a = arr;    
    }
    public MyClassIterator iterator() {
        return new MyClassIterator(this);
    }
}


    public class MyClassIterator implements Iterator<String> {
        private MyClass myclass = null;
        private int count = 0;
        public MyClassIterator(MyClass m) {
            myclass = m;    
        }

        public boolean hasNext() {
            return count < myclass.a.length;
        }
        public String next() {
            int t = count;
            count++;
            return myclass.a[t];
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    }   

哪一个更好?


答案 1

您几乎不应该在同一类中同时实现这两者。他们做不同的事情。迭代器自然是有状态的 - 当你使用它进行迭代时,它必须更新其世界观。但是,可迭代对象只需要能够创建新的迭代器。特别是,您可以让多个迭代器同时处理同一个原始可迭代器。IterableIterator

您目前的方法几乎没问题 - 我会更改实现的某些方面,但在职责分离方面也很好。


答案 2

你第一次尝试就走上了正轨。 只需要实现 ,这反过来又要求您提供要从 返回的实现。MyClassIterable<String>Iterator<String>Iterable<String>.iterator()

没有必要把外部放在,因为在大多数情况下,你甚至不需要直接使用(它被s上的语法隐式使用),在所有其他情况下,接口就足够了,除非你实际向实现添加额外的行为(你可能永远不需要这样做)。MyClassIteratorMyClassIterator<String>for .. in ..Iterable<String>

以下是我的做法,请参阅内联注释:

import java.util.Iterator;

class MyClass implements Iterable<String>{
    public String[] a=null; //make this final if you can
    public MyClass(String[] arr){
        a=arr; //maybe you should copy this array, for fear of external modification
    }

    //the interface is sufficient here, the outside world doesn't need to know
    //about your concrete implementation.
    public Iterator<String> iterator(){
        //no point implementing a whole class for something only used once
        return new Iterator<String>() {
            private int count=0;
            //no need to have constructor which takes MyClass, (non-static) inner class has access to instance members
            public boolean hasNext(){
                //simplify
                return count < a.length;
            }
            public String next(){
                return a[count++]; //getting clever
            }

            public void remove(){
                throw new UnsupportedOperationException();
            }
        };
    }
}