1

ユーザー入力(数字)を受け取るプログラムを作成する必要があり、プログラムはその数字を持ち、配列に検索を適用し、ユーザーが入力したインデックスと数字を照合して対応するタイトルを出力する必要があります。ただし、実行時には何も起こりません。コードにブレーカーを設定しましたが、for ループ (検索アルゴリズム) に問題があることに気付きました。私を助けて、私の検索アルゴリズムが間違っていることを教えてください。私がやろうとしているのは、ユーザーが入力した数を使用してインデックスと一致させ、インデックスに保存されている本のタイトルを出力することです。

       private void btnFindActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:  

    // declares an array 
   String[] listOfBooks = new String [101];

   // assigns index in array to book title 
   listOfBooks[1] = "The Adventures of Tom Sawyer"; 
   listOfBooks[2] = "Huckleberry Finn"; 
   listOfBooks[4] = "The Sword in the Stone";
   listOfBooks[6] = "Stuart Little";
   listOfBooks[10] = "Treasure Island";
   listOfBooks[12] = "Test";
   listOfBooks[14] = "Alice's Adventures in Wonderland";
   listOfBooks[20] = "Twenty Thousand Leagues Under the Sea";
   listOfBooks[24] = "Peter Pan";
   listOfBooks[26] = "Charlotte's Web";
   listOfBooks[31] = "A Little Princess";
   listOfBooks[32] = "Little Women";
   listOfBooks[33] = "Black Beauty";
   listOfBooks[35] = "The Merry Adventures of Robin Hood";
   listOfBooks[40] = "Robinson Crusoe";
   listOfBooks[46] = "Anne of Green Gables";
   listOfBooks[50] = "Little House in the Big Woods";
   listOfBooks[52] = "Swiss Family Robinson";
   listOfBooks[54] = "The Lion, the Witch and the Wardrobe";
   listOfBooks[54] = "Heidi";
   listOfBooks[66] = "A Winkle in Time";
   listOfBooks[100] = "Mary Poppins";

    // gets user input 
    String numberInput = txtNumberInput.getText();
    int number = Integer.parseInt(numberInput);

    // Linear search to match index number  and user input number
        for(int i = 0; i < listOfBooks.length - 1; i++) {
        if (listOfBooks.get(i) == number) {
        txtLinearOutput.setText(listOfBooks[i]);
        break; 
        }


    }

※if文のlistOfBooks.getに問題があります。また、バイナリ メソッドを使用して同じ配列を検索するバイナリ検索を適用する必要があります。このタイプの二分探索を適用するには助けが必要です。

int 数値がインデックスと等しいかどうかを確認するステートメントを作成するにはどうすればよいですか?

次のコードは、適用する必要があるものの例にすぎないことに注意してください。変数はすべて例としてのものです。

public static Boolean binarySearch(String [ ] A, int left, int right, String V){
     int middle;

     if (left > right) {
         return false;
     }

     middle = (left + right)/2;
     int compare = V.compareTo(A[middle]);
     if (compare == 0) {
         return true;
     }
     if (compare < 0) {
         return binarySearch(A, left, middle-1, V);
     } else {
         return binarySearch(A, middle + 1, right, V);
     }
 }
4

5 に答える 5

4

for loop次のような番号を指定するだけで、状態を回避して確認できます。txtLinearOutput.setText(listOfBooks[number-1]);

コードを削除する

// Linear search to match index number  and user input number
for(int i = 0; i < listOfBooks.length - 1; i++) {
    if (listOfBooks.get(i) == number) {
     txtLinearOutput.setText(listOfBooks[i]);
     break; 
}

try{
     int number = Integer.parseInt(numberInput);
     if(number>0 && number<101){
       txtLinearOutput.setText(listOfBooks[number-1]);
     }else{
        // out of range
     }
 }catch(Exception e){
   // handle exception here
 }
于 2015-08-19T05:54:00.503 に答える
2

比較if (listOfBooks.get(i) == number)するのは間違っていますif (i == number)。要素の位置を比較する必要があるため、比較する必要があります。

于 2015-08-19T05:57:28.223 に答える
0

線形検索コードは次のようになります

try{
txtLinearOutput.setText(listOfBooks[yourNumber]);
}
catch(IndexOutOfBoundsException ie){
// prompt that number is not an index
}
catch(Exception e){
// if any other exception is caught
}
于 2015-08-19T06:09:47.447 に答える
0

これは二分探索の答え​​ではありません。の単なる実装ですHashMap。それを見てください。

HashMap<String, Integer> books = new HashMap();
books.put("abc", 1);
books.put("xyz", 2);
books.put("pqr", 3);
books.put("lmo", 4);

System.out.println(books.getValue("abc");

組み込みの BinarySearch を使用します。

String []arr = new String[15];
arr[0] = "abc";
arr[5] = "prq";
arr[7] = "lmo";
arr[10] = "xyz";
System.out.println(Arrays.binarySearch(arr, "lmo"));

Strings二分探索を使用して比較する方法。

String[] array = new String[4];
        array[0] = "abc";
        array[1] = "lmo";
        array[2] = "pqr";
        array[3] = "xyz";
        int first, last, middle;
        first = 0;
        last = array.length - 1;
        middle = (first + last) / 2;
        String key = "abc";
        while (first <= last) {
            if (compare(array[middle], key))
                first = middle + 1;
            else if (array[middle].equals(key)) {
                System.out.println(key + " found at location " + (middle) + ".");
                break;
            } else {
                last = middle - 1;
            }
            middle = (first + last) / 2;
        }
        if (first > last)
            System.out.println(key + " is not found.\n");

    }

    private static boolean compare(String string, String key) {
        // TODO Auto-generated method stub
        for (int i = 0; i < Math.min(string.length(), key.length()); ++i)
            if (string.charAt(i) < key.charAt(i))
                return true;
        return false;

    }
于 2015-08-19T06:02:50.963 に答える