本文翻译自:How to iterate through SparseArray?
Is there a way to iterate over Java SparseArray (for Android) ? 有没有办法迭代Java SparseArray(适用于Android)? I used sparsearray
to easily get values by index. 我使用sparsearray
轻松地通过索引获取值。 I could not find one. 我找不到一个。
#1楼
参考:https://stackoom.com/question/XYxX/如何遍历SparseArray
#2楼
If you don't care about the keys, then valueAt(int)
can be used to while iterating through the sparse array to access the values directly. 如果您不关心键,则可以使用valueAt(int)
迭代稀疏数组以直接访问值。
for(int i = 0, nsize = sparseArray.size(); i < nsize; i++) {
Object obj = sparseArray.valueAt(i);
}
#3楼
Ooor you just create your own ListIterator: 您只需创建自己的ListIterator:
public final class SparseArrayIterator<E> implements ListIterator<E> {
private final SparseArray<E> array;
private int cursor;
private boolean cursorNowhere;
/**
* @param array
* to iterate over.
* @return A ListIterator on the elements of the SparseArray. The elements
* are iterated in the same order as they occur in the SparseArray.
* {@link #nextIndex()} and {@link #previousIndex()} return a
* SparseArray key, no