【问题标题】:Finding multiple search results in an array in Java在Java中的数组中查找多个搜索结果
【发布时间】:2010-08-26 19:54:20
【问题描述】:

我有一个由名字组成的数组。我有一个搜索功能,可以搜索数组的元素,它工作正常。但是,对于数组的多个元素,我不知道如何打印返回了多少结果。例如,如果在我的示例中找到“John”,我不知道如何表明找到了多个结果。有人请帮助我。对于找到的每个结果,我需要获得“计数”以增加 1 倍。这是我的代码: `

import java.util.*;

class Search {
    public static void main(String[] args) {

    Scanner search = new Scanner(System.in); 
       String[] firstName = new String[]{"John", "Thomas", "Samuel", "Chris", "Daniel", "Joey", "David", "Joshua", "Michael", "John"};
       Arrays.sort(firstName);
       System.out.print("Enter a search term: ");
       String name = search.next();

       int i;

       boolean foundIt = false;

    search:
       for (i = 0; i < firstName.length; i++) {
           if (firstName[i] == name) {
                   foundIt = true;

               }
           }


       if (foundIt = true){
            System.out.println("Your search term of " + name + " produced " + count + " search results");
       }

       else {
           System.out.println("Your search term of " + name + " did not return any results");
       }
   }
}

【问题讨论】:

标签: java arrays search


【解决方案1】:

您可以将 boolean foundIt 更改为 int count,并在将 foundIt 设置为 true 的位置递增。

比如:

int count = 0;

search:
   for (i = 0; i < firstName.length; i++) {
       if (firstName[i] == name) {
               count++;
       }
   }


   if (count > 0){
        System.out.println("Your search term of " + name + " produced " + count + " search results");
   }

   else {
       System.out.println("Your search term of " + name + " did not return any results");
   }

【讨论】:

  • 简单,简洁,不为他们做任何人的功课!完美的答案。
  • 这里也一样:不要使用== 比较Java 中的字符串!
【解决方案2】:

试试这个代码:

    Scanner search = new Scanner(System.in);
    String[] firstName = {"John", "Thomas", "Samuel", "Chris", "Daniel", "Joey", "David", "Joshua", "Michael", "John"};
    Arrays.sort(firstName);
    System.out.print("Enter a search term: ");
    String name = search.next();

    int count = 0;
    for (String aFirstName : firstName) {
        if (aFirstName.equals(name)) {
            count++;
        }else if(count > 0){
            break;
        }
    }

    if(count > 0){
        System.out.println("Your search term of " + name + " produced " + count + " search results");
    }else{
        System.out.println("Your search term of " + name + " did not return any results");
    }

【讨论】:

    【解决方案3】:

    您可以将结果添加到 ArrayList,这样更容易理解。此外,您在 if 语句中分配变量并在字符串上使用 == 运算符,您应该使用 .equals()

    另一个突出的是代码中的标签,你应该远离它们,因为它们需要使用臭名昭著的break 语句。见 Dijkstra 的considered harmful.

    这样您之后还可以遍历找到的名称。

    看例子:

    import java.util.ArrayList;
    import java.util.Arrays;    
    import java.util.List;
    import java.util.Scanner;
    
    
    class Search {
        public static void main(String[] args) {
    
            Scanner search = new Scanner(System.in);
            String[] firstNames = new String[]{
                "John",
                "Thomas",
                "Samuel",
                "Chris",
                "Daniel",
                "Joey",
                "David",
                "Joshua",
                "Michael",
                "John"};
    
            Arrays.sort(firstNames);
            System.out.print("Enter a search term: ");
            String name = search.next();
    
            List<String> results = new ArrayList<String>();
            for (String s : firstNames) {
                if (s.equals(name)) {
                    results.add(s);
                }
            }
    
    
            if (results.size() > 0) {
                System.out.println("Your search term of " + name + " produced " + results.size() + " search results");
            } else {
                System.out.println("Your search term of " + name + " did not return any results");
            }
        }
    }
    

    【讨论】:

      【解决方案4】:
      private void searchName(){
      
      Employee[]e=toEmployee();
      for(int i=0;i<em.size();i++){
          if(e[i].getName().equals(tfs.getText())){
              tfi.setText(e[i].getId());
              tfn.setText(e[i].getName());
              tfh.setText(e[i].getHour()+"");
              tfr.setText(e[i].getRate()+"");
          }
      }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-05-09
        • 1970-01-01
        • 1970-01-01
        • 2017-11-17
        • 1970-01-01
        • 2016-02-06
        • 2015-09-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多