如何比较对象在一个ArrayList属性? [英] How to compare Objects attributes in an ArrayList?

查看:165
本文介绍了如何比较对象在一个ArrayList属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的Java和我已经用尽了我所有的现有资源来寻找答案。我想知道是否有可能访问对象第一个属性,看它是否一个特定的整数相匹配?

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?

例如,我想获得一个产品,是我的数据库内寻找它通过它的产品编号。因此,如果我创建两个产品,例如,产品iPad的=新的产品(12345,iPad是,125.0,系code.COMPUTER); 产品iPod的=新的产品(12356,iPod的,125.0,系code.ELECTRONICS); (我已经包含低于该产品类),并将它们添加到一个ArrayList这样的如,列表与LT;产品与GT;产品=新的ArrayList<产品及GT;(); 我怎样才能通过此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循环,但我无法弄清楚如何访问产品类中的getId()方法来比较其产品ID参数?

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;
    }

}

请让我知道

推荐答案

您已经得到了产品出的列表&LT;产品&GT; 在下面的语句:

You have already got the Product out of the List<Product> in the following statement:

System.out.println(products.get(i));

现在,你已经得到了产品,现在得到它的标识的,你可以调用它的的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 的类型为 INT 。你并不需要一个包装类型那里。

Also, you should change the type of productId from Integer to int. You don't need a wrapper type there.

这篇关于如何比较对象在一个ArrayList属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆