diff options
| author | Tejas Khorana <khorana@google.com> | 2016-07-13 14:00:09 -0700 |
|---|---|---|
| committer | Tejas Khorana <khorana@google.com> | 2016-07-15 10:51:18 -0700 |
| commit | f3b09b9c54b5dfcc84ca89b4c383871313fa60b5 (patch) | |
| tree | c00e77a7ed1dca93c9b743ddcd8dccea71441a43 /core/java/android/util/SparseArray.java | |
| parent | de8ca1697a4036c82b89b45adb258a7cf365677b (diff) | |
indexOfValueByValue now uses .equals
To address the issue that indexOfValue does not compare objects by
value (using .equals). I have made a method that does the indexOfValue
operation but instead comparing Objects using equals. New method created
as it was too late to change indexOfValue itself.
Change-Id: Ie58ce279aca74ef25ce151d8f8bde769f644f0d0
Diffstat (limited to 'core/java/android/util/SparseArray.java')
| -rw-r--r-- | core/java/android/util/SparseArray.java | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/core/java/android/util/SparseArray.java b/core/java/android/util/SparseArray.java index 34e6f04f0404..c76666069d18 100644 --- a/core/java/android/util/SparseArray.java +++ b/core/java/android/util/SparseArray.java @@ -346,10 +346,40 @@ public class SparseArray<E> implements Cloneable { gc(); } - for (int i = 0; i < mSize; i++) - if (mValues[i] == value) + for (int i = 0; i < mSize; i++) { + if (mValues[i] == value) { return i; + } + } + + return -1; + } + + /** + * 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 this method uses {@code equals} unlike {@code indexOfValue}. + */ + public int indexOfValueByValue(E value) { + if (mGarbage) { + gc(); + } + for (int i = 0; i < mSize; i++) { + if (value == null) { + if (mValues[i] == null) { + return i; + } + } else { + if (value.equals(mValues[i])) { + return i; + } + } + } return -1; } |
