我有這個Player
類,它實現Comparable
接口。然後,我有Player
的ArrayList
秒。我試圖用binarySearch()
的Player
單子上找到一個Player
,但Java是給了我一個「cannot find symbol: method binarySearch(java.util.ArrayList< Player>,Player)
」。爲什麼不Collections.binarySearch()與此相當的工作?
這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()這份名單上的,它的工作原理。
,你能否告訴我們,調用Collections.binarySearch的代碼? – daveb