Java arraylist,试图搜索?

时间:2014-12-01 05:50:57

标签: java methods arraylist return void

这些是我给出的方法,它们都是无效的。我想使用适当的循环访问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){

    }

3 个答案:

答案 0 :(得分:1)

public void displaySearchResults(ArrayList<Vehicle> results){
    for(Vehicle vehicle : results){
        System.out.println(vehicle.getModel()+ " of " +vehicle.getYear()+ " of " + vehicle.getSellingPrice());
    }
}

答案 1 :(得分:0)

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

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());
}

答案 2 :(得分:0)

你可以创建一个对象,它的构造函数包含一个车辆数组,并且有一个名为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)它不是线程安全的。