How to iterate through SparseArray?

后端 未结 10 1742
野的像风
野的像风 2020-11-30 18:16

Is there a way to iterate over Java SparseArray (for Android) ? I used sparsearray to easily get values by index. I could not find one.

10条回答
  •  孤独总比滥情好
    2020-11-30 18:42

    Ooor you just create your own ListIterator:

    public final class SparseArrayIterator implements ListIterator {
    
    private final SparseArray 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, not an index! To get the index, call
     *         {@link android.util.SparseArray#indexOfKey(int)}.
     */
    public static  ListIterator iterate(SparseArray array) {
        return iterateAt(array, -1);
    }
    
    /**
     * @param array
     *            to iterate over.
     * @param key
     *            to start the iteration at. {@link android.util.SparseArray#indexOfKey(int)}
     *            < 0 results in the same call as {@link #iterate(android.util.SparseArray)}.
     * @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, not an index! To get the index, call
     *         {@link android.util.SparseArray#indexOfKey(int)}.
     */
    public static  ListIterator iterateAtKey(SparseArray array, int key) {
        return iterateAt(array, array.indexOfKey(key));
    }
    
    /**
     * @param array
     *            to iterate over.
     * @param location
     *            to start the iteration at. Value < 0 results in the same call
     *            as {@link #iterate(android.util.SparseArray)}. Value >
     *            {@link android.util.SparseArray#size()} set to that size.
     * @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, not an index! To get the index, call
     *         {@link android.util.SparseArray#indexOfKey(int)}.
     */
    public static  ListIterator iterateAt(SparseArray array, int location) {
        return new SparseArrayIterator(array, location);
    }
    
    private SparseArrayIterator(SparseArray array, int location) {
        this.array = array;
        if (location < 0) {
            cursor = -1;
            cursorNowhere = true;
        } else if (location < array.size()) {
            cursor = location;
            cursorNowhere = false;
        } else {
            cursor = array.size() - 1;
            cursorNowhere = true;
        }
    }
    
    @Override
    public boolean hasNext() {
        return cursor < array.size() - 1;
    }
    
    @Override
    public boolean hasPrevious() {
        return cursorNowhere && cursor >= 0 || cursor > 0;
    }
    
    @Override
    public int nextIndex() {
        if (hasNext()) {
            return array.keyAt(cursor + 1);
        } else {
            throw new NoSuchElementException();
        }
    }
    
    @Override
    public int previousIndex() {
        if (hasPrevious()) {
            if (cursorNowhere) {
                return array.keyAt(cursor);
            } else {
                return array.keyAt(cursor - 1);
            }
        } else {
            throw new NoSuchElementException();
        }
    }
    
    @Override
    public E next() {
        if (hasNext()) {
            if (cursorNowhere) {
                cursorNowhere = false;
            }
            cursor++;
            return array.valueAt(cursor);
        } else {
            throw new NoSuchElementException();
        }
    }
    
    @Override
    public E previous() {
        if (hasPrevious()) {
            if (cursorNowhere) {
                cursorNowhere = false;
            } else {
                cursor--;
            }
            return array.valueAt(cursor);
        } else {
            throw new NoSuchElementException();
        }
    }
    
    @Override
    public void add(E object) {
        throw new UnsupportedOperationException();
    }
    
    @Override
    public void remove() {
        if (!cursorNowhere) {
            array.remove(array.keyAt(cursor));
            cursorNowhere = true;
            cursor--;
        } else {
            throw new IllegalStateException();
        }
    }
    
    @Override
    public void set(E object) {
        if (!cursorNowhere) {
            array.setValueAt(cursor, object);
        } else {
            throw new IllegalStateException();
        }
    }
    }
    

提交回复
热议问题