我有这个Player
实现Comparable
接口的类。然后我有一个ArrayList
s Player
。我试图binarySearch()
在 s 的列表中Player
找到一个Player
,但 Java 给了我一个“ cannot find symbol: method binarySearch(java.util.ArrayList< Player>,Player)
”。
这是 Player 类:
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);
}
}
奇怪的是,当我在同一个列表上尝试 Collections.sort() 时,它起作用了。