Java ListIterator clarification

I am working on implementing a method that checks for number of max number consecutive equal elements in an ArrayList:

public class ArrayReader<E> {

    public int getMaxConsecutiveEqualElements(ArrayList<E> array){

        if (array == null){
            throw new IllegalArgumentException("Array is null");
        }
        if (array.size() == 0){
            throw  new IllegalArgumentException("Array has 0 elements");
        }

        int max = 1;
        int currentMax = 0;
        int index = 0;
        ListIterator<E> listIterator = array.listIterator(0);

        while (listIterator.hasNext()){
            E currentItem = array.get(index);
            E nextItem = listIterator.next();

            System.out.println("Current item: "
                    + "index (" + listIterator.previousIndex() + ") "
                    + currentItem.toString() + "   Next item: "
                    + "index (" + (listIterator.previousIndex() + 1) + ") "
                    + nextItem.toString());

            if (currentItem.equals(nextItem)){
                currentMax++;
                if (currentMax > max){
                    max = currentMax;
                }
            } else {
                currentMax = 1;
            }

            index++;
        }

        return max;
    }

}

public static void main(String[] args){

        ArrayList<Integer> array = new ArrayList<>();
        array.add(2);
        array.add(2);
        array.add(2);
        array.add(5);
        array.add(5);
        array.add(5);
        array.add(5);

        ArrayReader<Integer> intArrayReader = new ArrayReader<>();
        System.out.println(intArrayReader.getMaxConsecutiveEqualElements(array));

    }

However, the output I am getting indicates that it isn't truly comparing the current element to the next:

Current item: index (0) 2   Next item: index (1) 2
Current item: index (1) 2   Next item: index (2) 2
Current item: index (2) 2   Next item: index (3) 2
Current item: index (3) 5   Next item: index (4) 5
Current item: index (4) 5   Next item: index (5) 5
Current item: index (5) 5   Next item: index (6) 5
Current item: index (6) 5   Next item: index (7) 5
7

What is wrong with this implementation?

Jon Skeet
people
quotationmark

However, the output I am getting indicates that it isn't truly comparing the current element to the next

Indeed, it will be comparing one item with itself in each case.

After all, you start with index = 0 and on the first iteration you use array.get(index) and listIterator.next(), both of which will return the first element.

A better approach (IMO) would be to get rid of the index part entirely, and even remove the ListIterator bit. Just use:

Iterator<E> iterator = array.iterator();
if (!iterator.hasNext()) {
    return 0;
}
E current = iterator.next();
while (iterator.hasNext()) {
    E next = iterator.next();
    // Do comparisons here
    current = next;
}

Then you can change your method to be much more general:

public int getMaxConsecutiveEqualElements(Iterable<E> sequence)

You can't take the count now, of course - but you can throw an exception instead of returning 0 if the first call to hasNext() returns false, if you want.

people

See more on this question at Stackoverflow