如何使用arraylist创建一种方法来返回每种产品的饮料数量?

时间:2016-12-13 01:50:25

标签: java sorting object arraylist

我是初学者,我不知道如何访问我已建立的arraylist以便能够返回每种产品的饮料数量(例如4个Cokes)

P.S。该产品是我的其他类的名称。

到目前为止,这是我的代码:

let c = &mut a as *mut [[u8; 4]; 3];

1 个答案:

答案 0 :(得分:2)

我认为这可能会有所帮助。试试看你得到了什么。实现作为VendingMachine类的方法。

public int getProductCount(String pName) throws IllegalArgumentException {
    int count = 0;

    if (pName == null) {
        throw new IllegalArgumentException();
    }

    for (Product item : this.vending) {
        //Assuming the instance variable of your Product class is called name
        //Also assuming name is private and you have a getter method in your Product class
        if (item.getName().equalsIgnoreCase(pName)) {
            count++;
        }
    }
    return count;
}