如何比较 ArrayList 中的对象属性? [英] How to compare Objects attributes in an ArrayList?
问题描述
我对 Java 还很陌生,我已经用尽了我当前的所有资源来寻找答案.我想知道是否可以访问 Objects first 属性以查看它是否与特定整数匹配?
I am fairly new to Java and I have exhausted all of my current resources to find an answer. I am wondering if it possible to access an Objects first property to see if it matches a particular integer?
例如,我试图通过搜索产品 ID 来获取数据库中的产品.因此,如果我创建两个产品,例如 Product ipad = new Product(12345, "iPad", 125.0, DeptCode.COMPUTER);
和 Product ipod = new Product(12356, "iPod", 125.0, DeptCode.ELECTRONICS);
(我在下面包含了这个 Product 类),并将它们添加到一个 Arraylist,例如 List
我怎样才能遍历这个 ArrayList 以便通过它的 ID 找到那个产品?这是我正在研究的方法:
For example, I am trying to obtain a Product that is within my Database by searching for it by it's Product ID. Therefore, if I create two products such as, Product ipad = new Product(12345, "iPad", 125.0, DeptCode.COMPUTER);
and Product ipod = new Product(12356, "iPod", 125.0, DeptCode.ELECTRONICS);
(I have included this Product class below), and add them to an Arraylist such as, List<Product> products = new ArrayList<Product>();
how can I loop through this ArrayList in order to find that product by its ID?
This is the method I am working on:
List<Product> products = new ArrayList<Product>();
@Override
public Product getProduct(int productId) {
// TODO Auto-generated method stub
for(int i=0; i<products.size(); i++){
//if statement would go here
//I was trying: if (Product.getId() == productId) {
System.out.println(products.get(i));
}
return null;
}`
我知道我可以在 for 循环中包含条件语句,但我不知道如何访问 Product 类中的 getId() 方法以将其与 productId 参数进行比较?
I know that I can include a conditional statement in the for loop but I cant figure out how to access the getId() method in the Product class to compare it the productId parameter?
package productdb;
public class Product {
private Integer id;
private String name;
private double price;
private DeptCode dept;
public Product(String name, double price, DeptCode code) {
this(null, name, price, code);
}
public Product(Integer id, String name, double price, DeptCode code) {
this.id = id;
this.name = name;
this.price = price;
this.dept = code;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public DeptCode getDept() {
return dept;
}
public void setDept(DeptCode dept) {
this.dept = dept;
}
public void setName(String name) {
this.name = name;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
String info = String.format("Product [productId:%d, name: %s, dept: %s, price: %.2f",
id, name, dept, price);
return info;
}
}
请告诉我
推荐答案
您已经在以下语句中从 List
中获取了 Product
:
You have already got the Product
out of the List<Product>
in the following statement:
System.out.println(products.get(i));
现在,您已经获得了 Product
,现在要获取它的 id,您只需调用它的 getId()
方法:>
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 中的对象属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!