1
我有一个文本文件中的名字列表:MARY,PATRICIA,LINDA,BARBARA,ELIZABETH,JENNIFER,MARIA,我把它们放入一个字符串数组中。我想能够搜索阵列,但我遇到了问题。我想使用快速排序算法对其进行排序,并使用二进制搜索进行搜索,但我试图让一些简单的工作。如果我搜索 “MARY” 的结果将是 “MARY是不是”String Array搜索输入
public class NameSearch {
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
FileReader woMen = new FileReader("names.txt");
String[] womenArray;
womenArray = new String[64];
BufferedReader reader = new BufferedReader(woMen);
String line = null;
for (int j=0;j<womenArray.length;j++)
{
womenArray[j] = reader.readLine();
}
reader.close();
int x = 0, y =0;
System.out.println("Name?");
Scanner keyboard = new Scanner(System.in);
String input = keyboard.nextLine();
for(int i=0;i<womenArray.length;i++) {
if (input == womenArray[i]) {
System.out.println("Found! "+input);
}
System.out.println(womenArray[i]+" is not it");
}
}
}
嗯......我将如何使用Arrays.Binary搜索这个?我将如何获得成功? – th30d0rab1e
所以排序后(Arrays.sort(....)),并从用户接受输入后,你不写for循环。你只需写int location = Arrays.binarySearch(womenArray,input); –
噢很酷,那么我打电话给显示女人的数组[位置]作为结果? – th30d0rab1e