Reputation: 3775
I have an array of strings which is sorted by default. I want a binary search over this list in java. Is there any buit-in binary search function for strings in java?
Upvotes: 0
Views: 2971
Reputation: 5
what if they dont have?
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
.
.
.
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
.
.
.
}
Upvotes: 0
Reputation: 86764
Both the Arrays and Collections utility classes have binary-search methods.
Upvotes: 6