0

I have a code like the following.

public class DefaultIterator<E> implements Iterator<E> {
private E[] array;
private int i = 0;

public DefaultIterator(E[] array) {
    this.array = array;
}

@Override
public boolean hasNext() {
    return false;
}

@Override
public E next() {
    return array[i++];
}

@Override
public void remove() {

}

}

// here is my execution.
    public Iterator<String> createNewIterator(Iterator<String>... generalIterators)    {
    return new DefaultIterator<Iterator<String>[]>(generalIterators);
}

I am getting the compilation error at the execution code. can somebody explain why it is failing and how to fix it?

Thanks.

3
  • What is the compilation error? It will help if you can include it. Commented Jun 6, 2013 at 1:35
  • The constructor DefaultIterator<Iterator<String>[]>(Iterator<String>[]) is undefined Commented Jun 6, 2013 at 1:39
  • Mac, I am getting this. cannot convert from DefaultIterator<Iterator<String>> to Iterator<String> Commented Jun 6, 2013 at 1:40

2 Answers 2

3

So the complaint is that none of the generic types match up between the field declaration, the constructor declaration, and the method declaration.

You want:

public Iterator<String> createNewIterator(String... generalIterators)    {
    return new DefaultIterator<String>(generalIterators);
}
Sign up to request clarification or add additional context in comments.

1 Comment

I want 'Iterator<String>' for the return type.
0

Your return type is another one than expected! DefaultIterator<Iterator<String>[]> isn't compatible with Iterator<String> Choose DefaultIterator<Iterator<String>[]> as your return type, this should solve it.

Comments

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.