Skip to content

Instantly share code, notes, and snippets.

@rekire
Last active February 11, 2019 13:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save rekire/9810954 to your computer and use it in GitHub Desktop.
Save rekire/9810954 to your computer and use it in GitHub Desktop.
Customized SparseArray which allows using a iterator.
/**
* @copyright
* This code is licensed under the Rekisoft Public License.
* See http://www.rekisoft.eu/licenses/rkspl.html for more informations.
*/
/**
* @package eu.rekisoft.android.util
* This package contains utils provided by [rekisoft.eu](http://rekisoft.eu/).
*/
package eu.rekisoft.android.util;
import android.util.SparseArray;
import java.util.Iterator;
/**
* Customized SparseArray which allows using a iterator.
*
* @author rekire
* @copyright This code is licensed under the Rekisoft Public License.<br/>
* See http://www.rekisoft.eu/licenses/rkspl.html for more informations.
*/
public class IterableSparseArray<E> extends SparseArray<E> implements Iterable<E> {
public IterableSparseArray() {super();}
public IterableSparseArray(int cap) {super(cap);}
@Override
public Iterator<E> iterator() {
return new SparseIterator<E>(this);
}
private static final class SparseIterator<T> implements Iterator<T> {
private int pos = 0;
private SparseArray<T> array;
private SparseIterator(SparseArray<T> array) {
this.array = array;
}
@Override
public boolean hasNext() {
return pos < array.size();
}
@Override
public T next() {
return array.valueAt(pos++);
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment