如何在java中使用while循环生成搜索语句,并在对象';发现

如何在java中使用while循环生成搜索语句,并在对象';发现,java,Java,我一直被这个java搜索语句所困扰。 我试图搜索名为stock的产品数组,该数组在Stockmanager类中初始化,该类包含一个id字段、一个名称和一个库存级别。 这些产品对象在一个称为产品的单独类中创建 总经理: // A list of the products. private ArrayList<Product> stock; /** * Initialise the stock manager. */ public StockManager() { st

我一直被这个java搜索语句所困扰。 我试图搜索名为stock的产品数组,该数组在Stockmanager类中初始化,该类包含一个
id
字段、一个名称和一个库存级别。 这些产品对象在一个称为产品的单独类中创建

总经理:

// A list of the products.
private ArrayList<Product> stock;

/**

 * Initialise the stock manager.

 */
public StockManager()
{
    stock = new ArrayList<>();
}
产品中有一个用于检索产品对象id的访问器方法:

/**
 * @return The product's id.
 */
public int getID()
{
    return id;
}
现在在Stockmanager中,我有了搜索方法,但是如果我不使用for-each循环,这个方法会抱怨不兼容的数据类型,或者如果我使用for-each循环,它会抱怨没有返回语句

方法:

/**

 * Try to find a product in the stock with the given id.

 * @return The identified product, or null if there is none

 * with a matching ID.

 */
public Product findProduct(int id)
{
    int index = 0;
    boolean searching = true;

    for (Product item : stock)
    {  
       while(searching && index < stock.size())
       {
            if (item.getID() == id)
            {
                searching = false;
                return item;
            } else {
                index++;
            } 
            if(searching)
            {
                return null;
            } else {
                return item;
            }
        }
    }        
}
/**
*尝试在库存中找到具有给定id的产品。
*@返回已标识的产品,如果没有,则返回null
*使用匹配的ID。
*/
公共产品findProduct(int id)
{
int指数=0;
布尔搜索=真;
用于(产品项:库存)
{  
while(搜索和索引
在这个返回语句中必须有一个
while
循环,因为如果找到命中,我不需要该方法在数组中进一步查找。 请问,我做错了什么?

只要删除
如果(搜索)
条件

/**

     * Try to find a product in the stock with the given id.

     * @return The identified product, or null if there is none

     * with a matching ID.

     */
    public Product findProduct(int id){
        for (Product item : stock){  
           if (item.getID() == id){
                return item;
            }
        } 
      return null;
    }

为什么需要while循环?不需要while循环。

没有理由有两个嵌套循环,也不需要
索引和
搜索
变量

对于
循环,您只需要一个
。如果找到匹配的
产品
,则返回该产品。如果不是,则在循环结束时返回
null

public Product findProduct(int id)
{    
    for (Product item : stock) {  
        if (item.getID() == id) {
            return item;
        }
    }
    return null;    
}

在Java8中,您可以使用流API

public Optional<Product> findProduct(int id)
{
    return stock.stream().filter(item -> item.getID() == id).findAny();
}

您认为这到底是做什么的:if(搜索){returnnull;}else{returnitem;}?具体错误是什么?“它抱怨……”不是一个具体的错误。考虑需要返回报表的逻辑…如果您的任何一个循环从未被输入,该方法将返回什么?该方法必须返回某些内容。当您总是在循环的第一次迭代中返回时,为什么会有一个
while
循环?祝贺您,建议通过提供无法编译的代码来解决这个问题。不确定OP是否理解如何修复此问题,以便它能够正常工作您的“Java 8之前”版本不会编译
getID
返回
int
,而不是
整数,请使用
==
而不是
equals
。您完全正确,我只是不确定如果找到命中,该方法是否会继续搜索数组,我的错。
public Optional<Product> findProduct(int id)
{
    return stock.stream().filter(item -> item.getID() == id).findAny();
}
public Product findProduct(int id)
{
    for (Product item : stock)
    {  
       if (item.getID() == id) 
       {
                return item;
       }
    }   
    return null;     
}