我正在尝试使用 Arrays.binarySearch() 方法在字符串数组中查找字符串的索引,但是该方法似乎在查找字符串“Free”时返回位置整数“-5”。知道为什么会这样吗?
String[] names = {"Arken","Ben","Darklark", "Free","group"};
void changeFriends(String uname, boolean b)
{ // change a friend's "online" status
Arrays.sort(names);
int index = Arrays.binarySearch(names, uname);
System.out.println("NAME OF ONLINE USER IS AT INDEX:" + index + "Name:" + uname);
if(index > -1)
{
if(b == true)
{
loggedOn[index] = true;
}
else
{
loggedOn[index] = false;
}
}
// call method to update buttons
changeNameButtons();
}