Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
165 views
in Technique[技术] by (71.8m points)

java - how to update an element from specific index in ArrayList


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The type mismatch happens because product.setPurchaseQuantity(pPurchaseQuantity) returns void but productList.set(...) requires an object of type Product as its second argument.

To update the quantity, you need to get a Product from the productList and then operate on the retrieved object. There is no need to set this object again since you are changing an attribute of this object and the reference to it remains in the productList.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...