Java arraylist,尝试搜索?

标签 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){

    }

最佳答案

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

关于Java arraylist,尝试搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27222417/

相关文章:

java - "java.net.SocketException: Connection reset"在我的 Runnable 类中,为什么?

java - 使用 "B map(A a1,A a2)"将 Stream<A> 转换为 Stream<B>

c++ - 如果字段通过引用传递,则类字段可以在 const 方法内修改吗?

java - 由于返回太早,方法返回错误值

java - 文件遍历不返回绝对路径,仅返回文件名

c# - 将 IEnumerable 转换为 ArrayList O(N) 还是 O(1)?

Java - SHA-256 哈希 : Invalid AES key length : 64 bytes

Java:如何在不指定完整类型的情况下为函数参数指定类型

java - 识别一个字符串中的数字

java - 如何洗牌对