【发布时间】: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