As you can see in the picture bellow latLng
is equal to latlng1
, but indexOfValue()
returns different values.
Why is that?
As you can see in the picture bellow latLng
is equal to latlng1
, but indexOfValue()
returns different values.
Why is that?
Internally, SparseArray
keeps values in array of Object
s. In the indexOfValue(E value)
method the value
is compared with the array elements by reference (not logical value):
public int indexOfValue(E value) {
for (int i = 0; i < mSize; i++)
if (mValues[i] == value)
return i;
return -1;
}
Variables latLng
and latlng1
refer to different objects in memory, despite they're equal logically.
Without seeing more of your code, my guess is that their lat/lng values are the same, however, they are not the same object. If using the LatLng object in the Google APIs the equal method just compares the lat and long values, not that they are the same object.
So the sparsearray only contains one of the objects and returns -1 because the other doesn't exist in the sparsearray.