0

I have created two iterators for an array: the first runs the array by rows (iteratorRow) and then by columns and the second, first by columns and then by rows (iteratorColumn).

I have another class, Matrix, in which I must create two methods for performing iteration (iteratorRowColumn and iteratorColumnRow) that return iterators that have created to be accessible to other classes.

The array must implement the Iterable interface and may be configured (using a Boolean) which of the two iterators it shall be refunded by calling iterator () method.

How can I do that? Do I have to do some getters methods? Something like this?

public Iterator iteratorRowColumn () {
    return new iteratorRow;
}
2
  • 1
    Possible duplicate of Iterator column and row array Java Commented Nov 17, 2015 at 12:47
  • 1
    "The array must implement the Iterable interface and may be configured (using a Boolean) which of the two iterators it shall be refunded by calling iterator () method." - You create the object new 2DArray(true) and depending on the parameter, overriden method iterator() will return either one or the other. Commented Nov 17, 2015 at 12:47

2 Answers 2

1

I think that the last sentence of assignment explains a problem very well. I am not sure what part of it is unclear so let me explain in detail:

The array must implement the Iterable interface

public class Matrix<T> implements Iterable<T>

may be configured (using a Boolean)

public Matrix(boolean defaultRowColumnIterator) {
    this.defaultRowColumnIterator = defaultRowColumnIterator;
}

which of the two iterators it shall be returning by calling iterator() method

@Override
public Iterator<T> iterator() {
    return defaultRowColumnIterator ? iteratorRowColumn() : iteratorColumnRow();
}

Here is a compilable example:

public class Matrix<T> implements Iterable<T> {

    T[][] array;
    boolean defaultRowColumnIterator;

    public Matrix(boolean defaultRowColumnIterator) {
        this.defaultRowColumnIterator = defaultRowColumnIterator;
    }

    // other methods and constructors

    public Iterator<T> iteratorRowColumn() {
        return null; // your current implementation
    }

    public Iterator<T> iteratorColumnRow() {
        return null; // your current implementation
    }

    @Override
    public Iterator<T> iterator() {
        return defaultRowColumnIterator ? iteratorRowColumn() : iteratorColumnRow();
    }

}
Sign up to request clarification or add additional context in comments.

Comments

0

Something like this:

public class Proba {

    Integer[][] array = new Integer[10][10];

    public class MyIter implements Iterator<Integer> {

        private Integer[] integers;
        private int index = 0;;

        public MyIter(Integer[] integers) {
            this.integers = integers;
        }

        @Override
        public boolean hasNext() {
            return index < integers.length -1 ;
        }

        @Override
        public Integer next() {
            return integers[index];
        }

        @Override
        public void remove() {
            //TODO: remove
        }
    }

    public static void main(String[] args) {
        Iterator<Integer> iter = new Proba().getIterator(1);
        while (iter.hasNext()) {
            System.out.println(iter.next());
        }
    }

    public Iterator<Integer> getIterator(int row) {
        return new MyIter(array[row]);
    }
}

3 Comments

"The array must implement the Iterable interface"
An array can't implement anything, You can create a MyArray or Matrix class and put the array into MyIter. That will be the same.
By implementing an array what I think is meant to implement a class that stores data in 2d array and allows to iterate over it. In case of your code, Proba would be the matrix class and it doesn't implement Iterable interface as required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.