微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从ArrayList中的特定索引更新元素

如何解决如何从ArrayList中的特定索引更新元素

如何从由另一个 ArrayList 组成的 Arrayist 更新元素?

我有一个名为 Product 的类,它由 nametypecurrentQuantitypurchaseQuantity 组成。我创建了一个名为 productList 的 ArrayList 来存储已添加Product 类中的每个产品的数据。

我的问题是当我去另一个班级并且我想更新索引 toPurchase 的特定产品的 i 元素时,我现在不知道如何去做。

我已经在课堂上声明了这一点:

private ArrayList<Product> productList;

我尝试这样做:

productList.set(productIndex,product.setPurchaseQuantity(pPurchaseQuantity)); 

但是在 product.setPurchaseQuantity(pPurchaseQuantity) 下方有一条红线提到了这一点:

必填类型:产品
提供:无效

到目前为止,这些是我的新课程中唯一的代码

解决方法

发生类型不匹配是因为 product.setPurchaseQuantity(pPurchaseQuantity) 返回 voidproductList.set(...) 需要一个 Product 类型的对象作为其第二个参数。

要更新数量,您需要从 Product获取一个 productList,然后对检索到的对象进行操作。无需再次设置此对象,因为您正在更改此对象的属性并且对其的引用保留在 productList 中。

Product product = productList.get(productIndex);
product.setPurchaseQuantity(pPurchaseQuantity);
,

你基本上没有改变arrayList中的任何东西,你想要做的是更新一个已经在这个arrayList中引用的对象,为此你可以获取这个对象并更新(见下文)

rowCount = Cells(Rows.Count,1).End(xlUp).Row
rowCount = rowCount - 1

range("A2").Select

For rowStart = 2 To rowCount
    If Not IsEmpty(ActiveCell.Offset(0,month + 2)) Then
        testValue = ActiveCell.Offset(0,month + 2).value
        MsgBox (rafA)
        MsgBox (testValue)
        If testValue > rafA Then
            'range(Cells(rowStart,month),Cells(rowStart,month + 8)).Copy_ 
                Destination:=inputwb.Sheets(1).range("I" & targetRow)
            MsgBox ("Cell Value is greater")
            'range(Cells(rowStart,1),8)).Copy_ 
                Destination:=inputwb.Sheets(1).range("A" & targetRow)
            'targetRow = targetRow + 1
        Else
            MsgBox ("Input value is greater")
        End If
    End If

    ActiveCell.Offset(1,0).Select
Next rowStart

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。