Array.binarySearch返回负数 [英] Array.binarySearch returning negative numbers
本文介绍了Array.binarySearch返回负数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Arrays.binarySearch()方法在字符串数组中查找字符串的索引,但是当查找字符串 Free时,该方法似乎返回位置整数 -5。知道为什么会这样吗?
I am trying to find the index of a string in a string array using the Arrays.binarySearch() method however the method seems to be return the postion integer "-5" when looking for the string "Free". Any idea why this would be?
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();
}
推荐答案
嗯,我只是跑了代码,我得到了索引3。
Hmm, i just ran your code and i got the index 3.
Arrays.sort(names);
int index = Arrays.binarySearch(names, "Free");
System.out.println(index);
您可能正在搜索免费
或免费
(带有空格),而不是免费
,在这种情况下,它返回-5。
you probably are searching free
or Free
(with a trailing white space) instead of Free
, in which case it returns -5.
这篇关于Array.binarySearch返回负数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文