我试图通过获取listOfProducts并对其进行迭代来筛选产品。

public List<Products> filterByPrice(int min,int max){

    List<Products> listofproducts = products.retrieveProducts();
    System.out.println(listofproducts +" size: " +listofproducts.size());

    for (Products productsVar : listofproducts) {
        if(productsVar.getPrice()>= min && productsVar.getPrice()<= max){
            return  listofproducts; //here how do i print the reduced listOfProducts
        }
    }
    return null;
}


可能是一种非常简单的方法,但却无法从列表中打印出减少的过滤产品

谢谢

最佳答案

您正在打印并返回整个列表,请尝试以下操作:

List<Products> listofproducts = New List<Products>();
//System.out.println(listofproducts +" size: " +listofproducts.size());

for (Products productsVar : products.retrieveProducts()){
    if(productsVar.getPrice()>= min && productsVar.getPrice()<= max){
       listofproducts.add(productsVar);
    }
return  listofproducts; // may return blank list if no products fall between min and max price range

关于java - 想要根据产品价格过滤产品并打印过滤后的产品,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35754276/

10-10 02:30