SparseArray中的indexOfValue与ArrayList中indexOf的区别

来源:互联网 发布:只有程序员才懂的笑话 编辑:程序博客网 时间:2024/06/08 16:13

SparseArray:

/**     * Returns an index for which {@link #valueAt} would return the     * specified key, or a negative number if no keys map to the     * specified value.     * <p>Beware that this is a linear search, unlike lookups by key,     * and that multiple keys can map to the same value and this will     * find only one of them.     * <p>Note also that unlike most collections' {@code indexOf} methods,     * this method compares values using {@code ==} rather than {@code equals}.     */    public int indexOfValue(E value) {        if (mGarbage) {            gc();        }        for (int i = 0; i < mSize; i++)            if (mValues[i] == value)                return i;        return -1;    }

ArrayList:

    @Override     public int indexOf(Object object) {        Object[] a = array;        int s = size;        if (object != null) {            for (int i = 0; i < s; i++) {                if (object.equals(a[i])) {                    return i;                }            }        } else {            for (int i = 0; i < s; i++) {                if (a[i] == null) {                    return i;                }            }        }        return -1;    }

SparseArray中的indexOfValue判断不是equals,如果SparseArray中的value为String类型时,indexOfValue(“xxx”)会返回-1。

0 0
原创粉丝点击