java - 将线性和二进制搜索应用于数组

标签 java arrays search binary-search linear-search

我必须创建一个接受用户输入(数字)的程序,然后该程序应具有该数字并对数组应用搜索并通过匹配索引和用户输入的数字输出相应的标题。但是在运行时,什么也没有发生。我在我的代码中设置了断路器,并注意到 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);
     }
 }

最佳答案

您可以避免 for 循环 并通过像这样给出数字来检查条件: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
 }

关于java - 将线性和二进制搜索应用于数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32087410/

相关文章:

java - 更改 Java printf 中的默认填充字符?

python - 从 3 维数组生成列表

java - 如何在 JavaFx 中将文本文件的行读入数组

sql - 如何在MySQL中实现关键字搜索?

search - Solr 关键字中的小写和大写

java - 如果 JPEG 为 10MB,java.awt.Image 将消耗多少内存?

java - Spinner 的条件语句

java - `java` 在此 Python 进程中找不到命令。请确保已安装 Java 并将 PATH 设置为 `java`

arrays - 从索引获取双数组中的值。 swift

javascript - 搜索并替换影响部分单词的正则表达式