java Array.binarySearch 返回负数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13734694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Array.binarySearch returning negative numbers
提问by Bradley
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?
我正在尝试使用 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();
}
回答by Brian Roach
If it's returning a negative value it's not found:
如果它返回负值,则找不到:
http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html
http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html
public static int binarySearch(Object[] a,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Object key)Returns:index of the search key, if it is contained in the array; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element greater than the key, or a.length if all elements in the array are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.
public static int binarySearch(Object[] a,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 对象键)返回:搜索关键字的索引,如果它包含在数组中;否则,(-(插入点) - 1)。插入点定义为将键插入数组的点:大于键的第一个元素的索引,如果数组中的所有元素都小于指定的键,则为 a.length。请注意,这保证当且仅当找到键时返回值将 >= 0。
Whatever you're passing in as uname
is not "Free"
. I highly suspect you're thinking case doesn't matter (or have trailing characters; whitespace or newline) ;)
无论你传入的uname
是不是"Free"
。我高度怀疑您认为大小写无关紧要(或有尾随字符;空格或换行符);)
回答by PermGenError
Hmm, i just ran your code and i got the index 3.
嗯,我刚刚运行了你的代码,我得到了索引 3。
Arrays.sort(names);
int index = Arrays.binarySearch(names, "Free");
System.out.println(index);
you probably are searching free
or Free
(with a trailing white space) instead of Free
, in which case it returns -5.
您可能正在搜索free
or Free
(带有尾随空格)而不是Free
,在这种情况下它返回-5。
回答by Siddharth Choudhary
Those who are still having this trouble like I was having earlier, and you are thinking that why can't anyone else see that it(Arrays.binarySearch) still have some problems because you have inserted the correct values and you still get the negative results. Well this answer is for those:
那些仍然像我之前一样遇到这个问题的人,你在想为什么其他人看不到它(Arrays.binarySearch)仍然有一些问题,因为你插入了正确的值,你仍然得到否定的结果. 那么这个答案是为那些人准备的:
It is because your array is not sorted, my friend. Even if it's of characters or strings
这是因为你的数组没有排序,我的朋友。即使是字符或字符串
For arrays.binarySearch the array should be sorted(I know, you must be thinking that How did I miss the most important thing (Well yeah, that happens)).
对于arrays.binarySearch,数组应该被排序(我知道,你一定在想我怎么会错过最重要的事情(嗯,是的,发生了))。