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