【问题标题】:Java: How to search an arraylist for a instance variable of an object in the arrayJava:如何在数组列表中搜索数组中对象的实例变量
【发布时间】:2016-04-14 12:23:54
【问题描述】:

以下代码让我非常头疼。它只是做我想做的事情,即接受输入的字符串,然后通过对象的分类(字符串)搜索书籍对象的数组列表,然后返回该书籍对象。目前,只要输入分类是列表中书籍的实际分类,下面的代码就可以工作。例如,找到了 TD999,因此将其返回。但是,如果我为该类键入一堆废话,例如 yhgfsdsfbv,它仍然会返回一本书,例如 TD345(似乎它变成了一本书,最初归类为 null,然后在程序运行时使用 setClassification 方法进行分类。

public Book searchClass(String inputClass){
    Book foundBook = null;
    for(Book findClass : bookStore){
        if(findClass.getClassification()!=null &&
 findClass.getClassification().equalsIgnoreCase(givenClass)) 
        return findClass;
            foundBook = findClass;
    }
    return foundBook;
}

我尝试修复它是这样的:

public Book searchClass(String inputClass) throws IllegalArgumentException{

    Book foundBook = null;
    for(Book findClass : bookStore){
        if(!(findClass.getClassification().equalsIgnoreCase(inputClass))){
                   throws new IllegalArgumentException("book not found")
        }
        else if(findClass.getClassification().equalsIgnoreCase(inputClass)){
            foundBook = findClass;
        }
    }
    return foundBook;
}

但是,即使我正确输入了列表中的分类,这也会每次都抛出异常。

我不知道如何解决这个问题来做我想做的事,我已经厌倦了很多其他方式,从来没有正常工作。我已经阅读了我所有的讲义、几本教科书和 oracle 网页,但不知道是什么导致了问题,因此我无法修复它。

【问题讨论】:

    标签: java arrays search


    【解决方案1】:

    让我们进行第一次尝试。如果你找到一本书,你会立即归还。因此,如果你通过整个循环而没有发现任何东西,你应该发出失败信号:返回 null,或者抛出异常。返回foundBook 是没有意义的,因为您知道自己没有找到任何东西。

    这意味着根本不需要foundBook 变量。

    public Book searchClass(String inputClass) {
        for (Book book: bookStore) {
            if (book.getClassification() != null &&
                book.getClassification().equalsIgnoreCase(inputClass))
            {
                return book;
            }
        }
    
        throw new IllegalArgumentException("book not found");
    }
    

    另一种编写方式是使用 Java 8 流。您可以让流处理循环,而不是显式循环。

    public Book searchClass(String inputClass) {
        return bookStore.stream()
            .filter(book -> book.getClassification() != null)
            .filter(book -> book.getClassification().equalsIgnoreCase(inputClass))
            .findAny()
            .orElseThrow(() -> new IllegalArgumentException("book not found"));
    }
    

    【讨论】:

      【解决方案2】:

      问题是,这里:

      public Book searchClass(String inputClass){
          if(findClass.getClassification()!=null && findClass.getClassification().equalsIgnoreCase(givenClass)) 
          return findClass;
              foundBook = findClass;
      }
      

      如果 if 条件为真,则返回该书,否则,如果不为真,则将 foundbook 设置为 findclass。

      然后,最后,您返回 foundbook,将始终返回,即使没有匹配的书。

      你必须这样做:

      for(Book findClass : bookStore){
          if(findClass.getClassification()!=null && findClass.getClassification().equalsIgnoreCase(givenClass)) 
          return findClass; //return the book
      }
      return null; // if no book was found, then return null.
      

      【讨论】:

        【解决方案3】:

        我将尝试解释代码,请阅读评论并修复您自己的代码,希望对您有所帮助。

        public Book searchClass(String inputClass) throws IllegalArgumentException{
        
            Book foundBook = null;
            for(Book findClass : bookStore){
               // if(!(findClass.getClassification().equalsIgnoreCase(inputClass))){
                 //          throws new IllegalArgumentException("book not found")
                //}//remove these because each loop checks that 
                if(findClass.getClassification().equalsIgnoreCase(inputClass)){
                    foundBook = findClass; // return findClass; // you do not need any more iteration you found it.
                }
            }
            return foundBook; //if you are here that means no match throw exception //throws new IllegalArgumentException("book not found")
        
        
        }
        

        【讨论】:

          【解决方案4】:
          public static int personThereAlready(String name, ArrayList<Person> people) {
              int index = -1;
              for(int i=0; i < people.size();i++) {
                  Person person = people.get(i);
                      
                  if (person.getName().equalsIgnoreCase(name)) {
                      index = i;
                      break;
                  }
              }
              return index;
          }
          

          【讨论】:

          • 您可以使用此方法返回人员数组列表中人员的索引。如果找不到您想要的人,该方法返回-1。有用且简单。我希望我能帮上忙!欢呼
          • 点评来源: 您还可以编辑答案并添加和解释吗?谢谢!
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-10-24
          • 2012-02-12
          • 1970-01-01
          • 2021-07-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多