在 main 方法中,我尝试了 .equals() 和 .compareTo(),它们在第 88、94 和 100 行都给了我同样的错误。当我使用 == 时,它会编译,但在它运行:
Type the word you're searching for. Or type -1 to stop: curse
sequentialSearch() : curse is not found (comparison=13040).
iterative binarySearch(): curse is not found (comparison=13).
recursive binarySearch(): curse is not found (comparison=13).
Type the word you're searching for. Or type -1 to stop:
我知道“诅咒”一词在我正在搜索的文本文件中。
这是我到目前为止的代码。
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
public class Search extends Object {
public static final String TO_STOP = "-1";
public static final int NOT_FOUND = -1;
public static int count1;
public static int count2;
public static int count3;
public Search()
{
count1 = 0;
count2 = 0;
count3 = 0;
}
public static int sequentialSearch(ArrayList<String> array, String value)
{
int low = 0;
int high = array.size() - 1;
for (int i = low; i <= high; i++){
count1++;
if (array.get(i) == value)
return i;
}
return NOT_FOUND;
}
public static int binarySearch(ArrayList<String> array, String value)
{
int low = 0;
int high = array.size() - 1;
while (low <= high) {
int mid = (low + high)/2;
if (array.get(mid) != value){
count2++;
high = mid - 1;
} else if (array.get(mid) != value){
count2++;
low = mid + 1;
} else
return mid;
}
return NOT_FOUND;
}
public static int binarySearch(ArrayList<String> array, int low, int high, String value)
{
if (low > high)
return NOT_FOUND;
int mid = (low + high)/2;
if (array.get(mid) != value){
count3++;
return binarySearch(array, low, mid-1, value);
} else if (array.get(mid) != value){
count3++;
return binarySearch(array, mid+1, high, value);
} else
return mid;
}
public static void main (String [] args) throws IOException
{
ArrayList<String> array = new ArrayList<String>();
File fn = new File("sortedWords.txt");
Scanner sc = new Scanner(fn);
Scanner keyboard = new Scanner(System.in);
boolean wantsToContinue = true;
while(sc.hasNextLine()){
String ln = sc.nextLine();
array.add(ln);
}
do {
System.out.print("Type the word you're searching for. Or type " + TO_STOP + " to stop: ");
String word2search = keyboard.nextLine();
if(word2search.equals(TO_STOP)){
wantsToContinue = false;
}
else {
int index;
index = sequentialSearch(array, word2search);
if (index.compareTo(NOT_FOUND))
System.out.println("sequentialSearch() : " + word2search + " is not found (comparison=" + count1 + ").");
else
System.out.println("sequentialSearch() : " + word2search + " is found in [" + index + "] (comparison=" + count1 + ").");
index = binarySearch(array, word2search);
if (index.compareTo(NOT_FOUND))
System.out.println("iterative binarySearch(): " + word2search + " is not found (comparison=" + count2 + ").");
else
System.out.println("iterative binarySearch(): " + word2search + " is found in [" + index + "] (comparison=" + count2 + ").");
index = binarySearch(array, 0, array.size()-1, word2search);
if (index.compareTo(NOT_FOUND))
System.out.println("recursive binarySearch(): " + word2search + " is not found (comparison=" + count3 + ").");
else
System.out.println("recursive binarySearch(): " + word2search + " is found in [" + index + "] (comparison=" + count3 + ").");
}
} while (wantsToContinue);
}
}