I made a class extending ArrayList called sortedWordList that holds and sorts Strings. I would like to implement a binary search to override the indexOf method.
I cannot use compareTo to compare my Strings. I understand that I am passing the type Object to compareTo, but I believe that has to be my parameter type in order to override ArrayList's indexOf method.
My binary search algorithm may be incorrect as I have not had a chance to debug it yet so you may ignore that.
@Override
public int indexOf(Object o) {
int min = 0;
int max = len - 1;
while (true){
int mid = (int)((min + max)/ 2);
if (this.get(mid).compareTo(o) == 0){
return(mid);
}
if (this.get(mid).compareTo(o) < 0){
min = mid;
}
else if (this.get(mid).compareTo(o) > 0){
max = mid;
}
if (max == mid) break;
}
return -1;
}