【问题标题】:Searching a Array for unique name using method使用方法在数组中搜索唯一名称
【发布时间】:2020-02-17 19:15:47
【问题描述】:

所以我今天参加了考试,虽然我找到了让我的代码工作的方法,但我不喜欢它

    public static void search(String name, Friend[] array) {

    for (int i = 0; i<array.length;i++) {

        if((array[i].getName()).equals(name)) {
            System.out.println(name+ " is found at position " +i+"\n");
        }
        else {
            System.out.print("\nName not in list\n");
        }
    }
}

所以我在这里所做的工作,我在 Friend 类型的数组中搜索我从 main 方法传递的名称。但是当它找到一个唯一的名字时我想停下来,所以虽然我喜欢我所拥有的,因为它显示了如果有多个名字我想只显示那些说包含约翰并忽略所有其他名字或者如果没有约翰,它只会打印一个“名称不在列表中”

【问题讨论】:

    标签: java arrays loops search


    【解决方案1】:

    您可以添加breakboolean 以避免反复打印相同的消息:

    boolean nameFound = false;
    for (int i = 0; i<array.length;i++) {
        if((array[i].getName()).equals(name)) {
            System.out.println(name+ " is found at position " +i+"\n");
            nameFound = true;
            break;
        }
    }
    
    if(!nameFound) System.out.print("\nName not in list\n");
    

    【讨论】:

    • Good Stuff Buddy,有没有办法让它在输入的两个名字都是 John 的情况下同时显示
    • 嘿@AidanWard,“两个名字都输入”是什么意思?
    • 说我输入了两个名字都恰好是 John,然后一旦循环找到第一个 John,它将退出,这是不正确的,因为第二个对象也称为 John
    • 我知道了,但是你的方法只接收一个名字String name,如果你想找到更多的名字,你需要改变你的方法签名,你明白了吗?
    • @AidanWard 如果您想为阵列中的每个“John”打印一条消息,只需删除 break; 行即可:)
    【解决方案2】:

    这里有两个问题:
    找到名称后不要打破 for 循环
    您在 else 部分打印“未找到”消息,而不是在 for 循环结束后打印,这就是您为每个朋友获取它的原因

    public static void search(String name, Friend[] array) {
        for (int i = 0; i<array.length;i++) {
            if((array[i].getName()).equals(name)) {
                System.out.println(name+ " is found at position " +i+"\n");
                return; // Stop if you found one
            }
        }
        System.out.print("\nName not in list\n"); // print that only after going through the entire list
    
    }
    

    【讨论】:

      【解决方案3】:

      一旦满足条件,使用break 中断循环。

      public static void search(String name, Friend[] array) {
         boolean found = false;
         for (int i = 0; i<array.length;i++) {
            if((array[i].getName()).equals(name)) {
               System.out.println(name+ " is found at position " +i+"\n");
               found = true;
               // break the loop
               // it will throw the control out of the loop
               break;
             }
          }
      
          // Not found print name not in the list
          if(!found){
             System.out.print("\nName not in list\n");
          }
       }
      

      【讨论】:

      • 你仍然会为每个错误的名字打印Name not on the list
      【解决方案4】:

      如果您不关心索引,这只是一个有趣的选择。它还返回是否找到该名称。

      public static boolean search(String name, Friend[] array) {
      
           boolean found = Arrays.stream(array)
                     .anyMatch(item -> item.getName().equals(name));
      
           if (found) {
              System.out.println(name+ " is found\n");
           }
           else {
             System.out.print("\nName not in list\n");
           }
      
           return found;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-14
        • 2019-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-25
        相关资源
        最近更新 更多