I have this Player
class which implements the Comparable
interface. Then I have an ArrayList
of Player
s. I m trying to use binarySearch()
on the list of Player
s to find one Player
, but Java is giving me a "cannot find symbol: method binarySearch(java.util.ArrayList< Player>,Player)
".
This the Player class:
class Player implements Comparable {
private String username;
private String password;
Statistics stats;
//Constructor, creates a new Player with a supplied username
Player(String name) {
username = name;
password = "";
stats = new Statistics();
}
//Accessor method to return the username as a String
String getName() {
return username;
}
String getPassword() {
return password;
}
void setPassword(String newPass) {
password = newPass;
}
//Method to change the username
void setName(String newName) {
username = newName;
}
public int compareTo(Object o) {
return username.compareTo(((Player)o).username);
}
}
Weird thing, when I try Collections.sort() on this same list, it works.