1

I'm creating a generic class SortedArray that implements Iterable. My big problem is that I'm lost as to what I need to return for this particular method, or for that matter if I'm missing a major step in implementing it. With this current code when I compile it in Unix I'm getting an error stating inconvertible types. Any feedback or information that could put me on my way to fixing this would be much appreciated. Thank you.

public class SortedArray< T extends Comparable< T > >
    extends Object
    implements Iterable < T > {

    private T[] internalArray;
    public Iterator<T> iterator() {
        return (Iterator<T>) Arrays.asList(internalArray).iterator();
    }
}
Luiggi Mendoza
  • 81,685
  • 14
  • 140
  • 306
GorillaSpoon
  • 25
  • 1
  • 4
  • 1
    What's `internalArray`? And why does your `class extends Object`? And what's the **full error**? Paraphrasing is not helpful. – Boris the Spider Oct 24 '14 at 20:58
  • Sorry about that. internalArray is used in other methods of the program, I was just running of ideas and this current code was just me grasping at straws. The full error was: error: invonvertible types return (Iterator) Arrays.asList(internalArray).iterator(); ^ required: Iterator found: Iterator where T is a type-variable: T extends Comprable declared in class SortedArray – GorillaSpoon Oct 24 '14 at 21:10
  • Update your answer - surely you can see that your comment is illegible. – Boris the Spider Oct 24 '14 at 21:14

2 Answers2

4

Usually, you create an inner class that implements Iterator interface and that's what you return in iterator method. Here's an example:

//removed extends Object. The compiler will add this info for you
public class SortedArray<T extends Comparable<T>> implements Iterable<T> {

    private T[] internalArray;

    private class SortedArrayIterator implements Iterator<T> {
        int currentIndex;
        public SortedArrayIterator() {
            this.currentIndex = 0;
        }
        @Override
        public boolean hasNext() {
            return currentIndex < maxSize;
        }
        @Override
        public T next() {
            return internalArray[currentIndex++];
        }
        @Override
        public void remove() {
            for (int i = currentIndex; i < internalArray.length - 1; i++) {
                internalArray[i] = internalArray[i+1];
            }
        }
    }

    public Iterator<T> iterator() {
        return new SortedArrayIterator();
    }
}

Not directly related to your question, but there are few problems when working with generic arrays. Make sure you create this private T[] internalArray using the right approach, which is storing Class<T> somewhere, or maintain a Comparable[] internalArray instead, as shown in How to create a generic array in Java?

Community
  • 1
  • 1
Luiggi Mendoza
  • 81,685
  • 14
  • 140
  • 306
0

There is only a warning for the redundant cast. When the cast is removed (and the redundant extends Object), it compiles:

public class SortedArray< T extends Comparable<T>>
    implements Iterable < T > {

    private T[] internalArray;
    public Iterator<T> iterator() {
        return Arrays.asList(internalArray).iterator();
    }
}

Compiles OK.

Bohemian
  • 365,064
  • 84
  • 522
  • 658