我正在尝试使用数组和 array.BinarySearch 函数验证密码和用户名。数组中的前两个用户名:bradley 和 john 使用函数 0 和 1 返回正确的位置。但是,当我尝试验证数组 jim 和 clarke 中的最后两个字符串时,binarySearch 函数返回用户名位于两次都在数组中的位置 -2 导致验证失败。有任何想法吗?
String[] names = {"bradley","john","jim","clarke"};
String[] passwords = {"password","password","test","test"};
int pos = Arrays.binarySearch(names, uname);
System.out.println("Found you in array:" + uname + "here:" + pos);
if(pos >= 0)
{
System.out.println("Validation password for:" + uname);
if(passwords[pos].equals(pword) && !loggedOn[pos])
{
}