Question

I am currently learning Java and for my inner classes practice, I played aroud the following code:

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());

    }
}

I am passing a EvenIterator object to the method print(DataStructureIterator), as far as I know a iterator can refer to a EvenIterator object(since DataStructureIterator is a implemented by EvenIterator), though hasNext() and setNextIndex(int) are in the same class the reference iterator is able to access only hasNext.

How can I fix this bug?

Was it helpful?

Solution

Even though you are passing EvenIterator to the method print(DataStructureIterator), it is silently getting casted into DataStructureIterator. So if the setNextIndex(int) method is not declared in DataStructureIterator, you will not be able to access it.

OTHER TIPS

The print method know its parameter is a DataStructureIterator. Is the method setNextIndex declared in that interface? If not, you must add it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top