Now, that you have got Product, now to get it's id, you can just call it's getId() method:if (product.get(i).getId() == productId) { // Return this product.}我还建议您使用增强的 for 循环而不是像这样的传统循环:I would also suggest you to use enhanced for-loop instead of the traditional loop like this:for (Product product: products) { // Now you have the product. Just get the Id if (product.getId() == productId) { return product; }}此外,您应该将 productId 的类型从 Integer 更改为 int.那里不需要包装器类型.Also, you should change the type of productId from Integer to int. You don't need a wrapper type there. 这篇关于如何比较 ArrayList 中的对象属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!