
org.divxdede.collection.ArrayIterator Maven / Gradle / Ivy
/*
* Copyright (c) 2010 ANDRE S?bastien (divxdede). All rights reserved.
* ArrayIterator.java is a part of this Commons library
* ====================================================================
*
* Commons library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or any later version.
*
* This is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see .
*/
package org.divxdede.collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
/* An iterator over an Array.
*
* @author Andr? S?bastien (divxdede)
*/
public class ArrayIterator implements Iterator {
private final E[] array;
private int index = 0;
public ArrayIterator(E... array) {
this.array = array;
}
/**
* Returns true if the iteration has more elements. (In other
* words, returns true if next would return an element
* rather than throwing an exception.)
*
* @return true if the iterator has more elements.
*/
public boolean hasNext() {
if (this.array == null) {
return false;
}
return index >= 0 && index < this.array.length;
}
/**
* Returns the next element in the iteration.
*
* @return the next element in the iteration.
* @exception NoSuchElementException iteration has no more elements.
*/
public E next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return array[index++];
}
/** Not supported operation
* @throws UnsupportedOperationException
*/
public void remove() {
throw new UnsupportedOperationException();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy