【问题标题】:Java arraylist, trying to search?Java arraylist,试图搜索?
【发布时间】:2015-01-29 02:32:15
【问题描述】:

这些都是给我的方法,都是无效的。我想使用适当的循环访问来自displaySearchResults 的结果,只读取数据。

有人知道我需要做什么才能从之前的 3 种搜索方法中提取结果吗?

/**
*   Searches inventory by model
*   @param model is the model you'd like to find
*/
public void searchByModel(String model){
    ArrayList<Vehicle> results = new ArrayList();
    for(int i = 0; i < vehicles.size(); i++){
        if(vehicles.get(i).getModel().equalsIgnoreCase(model)){
            results.add(vehicles.get(i));
        }
    }
}  

    /**
*   Searches inventory by year
*   @param year is the year you'd like to find
*/
public void searchByYear(int year){
    ArrayList<Vehicle> results = new ArrayList();
    for(int i = 0; i < vehicles.size(); i++){
        if(vehicles.get(i).getYear() == year){
            results.add(vehicles.get(i));
        }
    }
}

/**
*   Searches inventory by price
*   @param minPrice is the lowest price you'd like to search by
*   @param maxPrice is the highest price you'd like to search by
*/
public void searchByPrice(double minPrice, double maxPrice){
    ArrayList<Vehicle> results = new ArrayList();
    for(int i = 0; i < vehicles.size(); i++){
        if(vehicles.get(i).getSellingPrice() < maxPrice &&
        vehicles.get(i).getSellingPrice() > minPrice){
            results.add(vehicles.get(i));
        }
    }

}

/**
 *  @return Displays search results, unsure of how to get this working still
 */
public void displaySearchResults(ArrayList<Vehicle> results){
    for(Vehicle vehicle : results){

    }

【问题讨论】:

  • 按原样你不能在每个searchByXX方法之外使用results,它是一个局部变量
  • 告诉给你这个的人学习编程。这些方法实际上什么都不做。他们没用。他们做了一堆工作然后扔掉。

标签: java methods arraylist return void


【解决方案1】:
public void displaySearchResults(ArrayList<Vehicle> results){
    for(Vehicle vehicle : results){
        System.out.println(vehicle.getModel()+ " of " +vehicle.getYear()+ " of " + vehicle.getSellingPrice());
    }
}

【讨论】:

    【解决方案2】:

    更改您的搜索方法,以便它们实际返回结果:

    public List<Vehicle> searchByYear(int year){
        ArrayList<Vehicle> results = new ArrayList<>();
        for(int i = 0; i < vehicles.size(); i++){
            if(vehicles.get(i).getYear() == year){
                results.add(vehicles.get(i));
            }
        }
        return results;
    }
    

    现在显示时,您可以迭代实际搜索的结果:

    public void displaySearchResults(ArrayList<Vehicle> results){
        for(Vehicle vehicle : searchByYear(1991)){
            //display whatever you want from it
        }
        // do this with the other results
    }
    

    此外,如果您使用的是 java 8,则可以使用更优雅的函数式东西替换 for 循环:

    public List<Vehicle> searchByPrice(double min, double max){
        return vehicles.stream()
            .filter(v -> (v.getSellingPrice() > min && v.getSellingPrice() < max))
            .collect(Collectors.toList());
    }
    

    【讨论】:

      【解决方案3】:

      您可以创建一个对象,在其构造函数中接收车辆数组并有一个名为 results 的成员。

      public class WhyWouldYouDoThis {
          private List<Vehicle> results;
          public WhyWouldYouDoThis() {
          }
          public List<Vehicle> getResults() {
              return results;
          }
         /**
          *   Searches inventory by year
          *   @param year is the year you'd like to find
          */
         public void searchByYear(int year){
      
               results = new LinkedList<>();
            for(int i = 0; i < vehicles.size(); i++){
               if(vehicles.get(i).getYear() == year){
                  results.add(vehicles.get(i));
               }
             }
         }
      
      }
      

      现在有几件事情要记住这一点。 A)这很疯狂,因为您的方法正在返回结果。您当前的代码在 API/设计级别确实存在问题。 B) 它不是线程安全的。

      【讨论】:

      • 这是给我的,我不能随意改变它,我只是想知道如何让它发挥作用。我知道不理想..
      • 上面的例子是让它工作的唯一方法。如果你在做一个项目,而这是架构师/团队负责人所交付的,请去找管理层并说这是一项糟糕的工作。否则你做不到。
      猜你喜欢
      • 2010-11-02
      • 1970-01-01
      • 2015-05-26
      • 2011-05-07
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-01
      相关资源
      最近更新 更多