Java-需要帮助解决错误吗

Java-需要帮助解决错误吗,java,inner-classes,Java,Inner Classes,我目前正在学习Java,在我的内部类练习中,我使用了以下代码: public class DataStructure { // Create an array private final static int SIZE = 15; private int[] arrayOfInts = new int[SIZE]; public DataStructure() { // fill the array with ascending integer

我目前正在学习Java,在我的内部类练习中,我使用了以下代码:

public class DataStructure {

    // Create an array
    private final static int SIZE = 15;
    private int[] arrayOfInts = new int[SIZE];

    public DataStructure() {
        // fill the array with ascending integer values
        for (int i = 0; i < SIZE; i++) {
            arrayOfInts[i] = i;
        }
    }

    public void printEven() {

        // Print out values of even indices of the array
        DataStructureIterator iterator = this.new EvenIterator();
        while (iterator.hasNext()) {
            System.out.print(iterator.next() + " ");
        }
        System.out.println();
    }

    interface DataStructureIterator extends java.util.Iterator<Integer> { } 

    // Inner class implements the DataStructureIterator interface,
    // which extends the Iterator<Integer> interface

    private class EvenIterator implements DataStructureIterator {

        // Start stepping through the array from the beginning
        private int nextIndex = 0;

        public boolean hasNext() {

            // Check if the current element is the last in the array
            return (nextIndex <= SIZE - 1);
        }        

        public Integer next() {

            // Record a value of an even index of the array
            Integer retValue = Integer.valueOf(arrayOfInts[nextIndex]);

            // Get the next even element
            nextIndex += 2;
            return retValue;
        }
        public void setNextIndex(int i){
            nextIndex=i;
        }
    }
    public void print(DataStructureIterator iterator) {

    // Print out values of odd indices of the array
    //iterator = this.new EvenIterator();
        iterator.setNextIndex(1);//**This line giving me compiler error that setNextIndex is undefined for type DataStructure.DataStructureIterator **
        while (iterator.hasNext()) {
            System.out.print(iterator.next() + " ");
        }
        System.out.println();
    }

    public EvenIterator createNewObject(){
        return this.new EvenIterator();
    }


    public static void main(String s[]) {

        // Fill the array with integer values and print out only
        // values of even indices
        DataStructure ds = new DataStructure();
        System.out.println("Even Index");
        ds.printEven();
        System.out.println("Odd Index");
        ds.print(ds.createNewObject());

    }
}
公共类数据结构{
//创建一个数组
私有最终静态整数大小=15;
private int[]arrayOfInts=新的int[SIZE];
公共数据结构(){
//用升序整数值填充数组
对于(int i=0;ireturn(nextIndexprint
方法知道它的参数是一个
DataStructureIterator
setNextIndex
方法是在该接口中声明的吗?如果不是,你必须添加它。

即使你将
EvenIterator
传递给方法
print(DataStructureIterator)
,它将以静默方式转换到
数据结构迭代器中
方法未在
DataStructureIterator
中声明,您将无法访问它。

您可以共享DataStructureIterator接口吗?由于DataStructureIterator持有object EvenIterator,它应该能够访问setNextIndex,但编译器不知道。它只看到该方法(可以使用另一个不提供该方法的DataStructureIterator调用该方法)。要么在接口中声明该方法(preferrable),要么在print中执行显式转换。