Iterator to remove duplicate element The code implements custom iterator, which skips duplicate integers. The array expected is to be sorted.

public class GenericIterator&lt;K&gt; implements Iterator&lt;K&gt; {

    private K[] array; // Array will be set here
    private int index;

    GenericIterator(K[] array) {
        this.array = array;
        index = 0;
    }

    /*
     * 
     * 
     * (non-Javadoc)
     * 
     * @see java.util.Iterator#hasNext() /** Returns &lt;tt&gt;true&lt;/tt&gt; if this 
     * iterator has more elements when traversing the array in the forward
     * direction. (In other words, returns &lt;tt&gt;true&lt;/tt&gt; if &lt;tt&gt;next&lt;/tt&gt; would
     * return an element rather than throwing an exception.)
     * 
     * @return &lt;tt&gt;true&lt;/tt&gt; if the array iterator has more elements when
     * traversing the array in the forward direction.
     */

    @Override
    public boolean hasNext() {
        return !(array.length == index);
    }

    /*
     * 
     * 
     * (non-Javadoc)
     * 
     * @see java.util.Iterator#hasNext()
     * 
     * /** Returns the next element in the array. This method may be called
     * repeatedly to iterate through the array, or intermixed with calls to
     * &lt;tt&gt;previous&lt;/tt&gt; to go back and forth. (Note that alternating calls to
     * &lt;tt&gt;next&lt;/tt&gt; and &lt;tt&gt;previous&lt;/tt&gt; will return the same element
     * repeatedly.)
     * 
     * @return the next element in the array.
     * 
     */

    @Override
    public K next() {

        K outputElement = array[index++];
        if (index == 0) {
            index++;
        }

        while (hasNext()) {
            if (array[index] == array[index - 1]) {
                index++;
            } else {
                break;
            }
        }
        return outputElement;

    }
}
Not much to review, but, if I read the Java documentation, you don't put the complete class in it.

Because you didn't put the full class I come to the next thing :

K outputElement = array[index++];
if (index == 0) {
    index++;
}


What the hell are you doing?

index is instantiated at 0, the line before is index++ so index is never 0.



Second point, you missed some errors.
When you are at the last element and you ask next() you will get an OutOfBoundsException.

Rather, it's better to check if your at the last element and if so, just return null.

The OutOfBoundsException is an unchecked exception, so nobody who will use your class will expect and handle this error, and then the problems are coming.



Also add JavaDoc what the Iterator returns when you are at the last element.



Then another point; the constructor has this line :

index = 0;


It's obsolete.  An int default value is always 0.
Because it's not a static variable, it's instantiated when you create the class; so it's 0.



Generics:

With the new description in your question, a few more things come up.
I don't know if you know how to use generics or it's just a fault but:


You expect Integer so I should rename the class name to UniqueIntegerIterator or something like that.
Because public class GenericIterator&lt;K&gt; implements Iterator&lt;K&gt; means that you can insert any type of Object it has a lot more usage then Integer only.
If you want to make it only for Integer, the declaration should change to public class GenericIterator implements Iterator&lt;Integer&gt;.
Of course the K[] should be changed to Integer[]


Expect bad usage of your class.

You state that you expect a sorted integer array in the constructor. You may ask:


  What if this isn't the case? 




It's really important to ask yourself this question.
Normally, when you have some requirements, you check for that and throw an AssertionError when the requirements are not met.
In this case and in my own perception, I should sort the array in the constructor so I know the requirement is done.
Also, when I do this I rename the class again to UniqueSortedIntegerIterator so the people who are going to use this class understand they get unique values from a sorted collection, in this case an array.  The Object behind is Integer and it acts like an Iterator.