1

我在更改 ArrayList 中特定位置的值时遇到问题。更清楚地说,情况如下:

我想在一个列表中添加产品 ID,并在另一个列表中添加它们的计数。但是,如果用户选择了已经在 productList 中的产品,我想在 countList 更新中使用新选择的计数器进行计数。

这是代码:

public void compareArrays(){
    if(Variables.productsList.size()==0){
        Variables.productsList.add(product.id);
        Variables.countList.add(count.toString());
    }
    for(int index=0; index<Variables.productsList.size(); index++){
        if(Variables.productsList.get(index).equals(product.id)){

            Integer a = Integer.parseInt(Variables.countList.get(index))+count;
            Variables.countList.set(index, a.toString());

        }

    }
        Variables.productsList.add(product.id);
        Variables.countList.add(count.toString());
}

问题是它没有进入 for 循环,没有进行检查。只需将项目放入列表中。请帮忙。先谢谢了!!!

4

0 回答 0