Reputation: 258
lateinit var dataList: PairList<Car, People>
I want to retrieve specific position based on pair, so what I do is:
val pair = Pair(carObj, peopleObj)
val position = dataList.indexOf(pair as Pair<Car, People>)
But it give me an error of Type inference failed and asks me to explicitly show type. I think I've done it with as Pair<Car, People>
, but it says that it's a redundant statement. Any ideas on where I went totally wrong? :)
PairList class:
public class PairList<F, S> extends ArrayList<Pair<F, S>> {
public boolean add(F first, S second) {
return add(new Pair<>(first, second));
}
public void add(int index, F first, S second) {
add(index, new Pair<>(first, second));
}
@Override
public int indexOf(Object o) {
if (o instanceof Pair) {
return super.indexOf(o);
} else {
for (int i = 0; i < size(); i++) {
Pair<F, S> pair = get(i);
if (pair != null) {
if (pair.first.equals(o)) return i;
} else {
if (o == null) return i;
}
}
for (int i = 0; i < size(); i++) {
Pair<F, S> pair = get(i);
if (pair != null) {
if (pair.second.equals(o)) return i;
} else {
if (o == null) return i;
}
}
}
return -1;
}
public int indexOf(F first, S second) {
for (int i = 0; i < size(); i++) {
Pair<F, S> pair = get(i);
if (pair != null) {
if (pair.first.equals(first) && pair.second.equals(second)) {
return i;
}
} else {
if(first == null && second == null) {
return i;
}
}
}
return -1;
}
Upvotes: 0
Views: 297
Reputation: 712
You are using android.util.Pair
in PairList.java
and you created Pair(carObj, peopleObj)
using kotlin.Pair;
So replace android.util.Pair;
with import kotlin.Pair;
in PairList.java
everything will work.
Upvotes: 1
Reputation: 1857
Your PairList class is probably extending Pair class from android.util.Pair;
and when you are using that in your kotlin file it is using kotlin.Pair
. Caz of this you are getting Type inference failed.
To overcome this you have two solution
android.util.Pair
.Upvotes: 1