【问题标题】:How to compare Objects attributes in an ArrayList?如何比较 ArrayList 中的对象属性?
【发布时间】:2013-08-26 20:39:03
【问题描述】:

我对 Java 还很陌生,我已经用尽所有当前资源来寻找答案。我想知道是否可以访问 Objects first 属性以查看它是否与特定整数匹配?

例如,我试图通过按产品 ID 搜索数据库中的产品来获取它。因此,如果我创建了两个产品,例如 Product ipad = new Product(12345, "iPad", 125.0, DeptCode.COMPUTER);Product ipod = new Product(12356, "iPod", 125.0, DeptCode.ELECTRONICS);(我在下面包含了这个 Product 类),并将它们添加到一个 Arraylist 中,例如 List<Product> products = new ArrayList<Product>(); 我如何遍历这个 ArrayList 以便找到该产品的ID? 这是我正在研究的方法:

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 参数?

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

}

请告诉我

【问题讨论】:

    标签: java database arraylist


    【解决方案1】:

    为了比较 ArrayList 对象,请在您的 CustomObject 类中重写 equal 函数,例如 Employee。

    ArrayList<Employee> list;
    Employee emp; 
    //suppose you have some number of items in that list and emp object contains some value.
    int size=list.size();  
    for(int i=0;i<size;i++) {
        if(list.get(i).equals(emp)) {
            //todo perform operation on the basis of result.
        }
    }
    

    假设这是您的 Employee 类,在该类中您需要重写 equal 函数。

    class Employee{
        int age;
        String name;
    
        public int getAge() {
            return this.age;
        }
    
        public String getName() {
            return this.name;
        }
    
        public void setAge(int emp_age) {
            this.age=emp_age;
        }
    
        public void setName(String emp_name) {
            this.name=emp_name;
        }
    
        @Override
        public boolean equal(Employee emp) {
            boolean isEqual=false;
            if(emp!=null && emp instanceof Employee) {
                isEqual=(this.age==emp.age);
            }
            return isEqual;
        }
    
    
    
    }
    

    我希望这个解决方案能帮助您检查相等的值并比较这些值。

    谢谢

    【讨论】:

      【解决方案2】:

      根据您的评论行//I was trying: if (Product.getId() == productId) 我认为您跌倒的地方是使用Product(大写P)。你需要的是:

      if (products.get(i).getId() == productId)

      另外,您没有退回您找到的产品...

      该表单的问题在于 a)您必须在列表中找到产品两次(一次在条件中,然后在打印结果时再次 - 如果您退回产品,则第三次)并且 b)它会抛出如果您要查找的产品不在列表中,则会出现空指针异常。

      这是一种更好、更紧凑的方法:

      @Override
      public Product getProduct(int productId)
      {
        for(Product product : products)
        {
          if (productId == product.getId())
          {
            System.out.println(product.toString());
            return product;
          } 
        }
        return null;
      }
      

      【讨论】:

        【解决方案3】:

        您已经在以下语句中从List&lt;Product&gt; 中获得了Product

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

        现在,您已经获得了Product,现在要获得它的id,您只需调用它的getId() 方法即可:

        if (product.get(i).getId() == productId) {
            // Return this product.
        }
        

        我还建议您使用增强的 for 循环,而不是像这样的传统循环:

        for (Product product: products) {
            // Now you have the product. Just get the Id
            if (product.getId() == productId) {
                return product;
            }
        }
        

        此外,您应该将 productId 的类型从 Integer 更改为 int。您不需要那里的包装器类型。

        【讨论】:

        • == 比较引用而不是 Object 和 Integer 上的值是一个对象
        • @YasirShabbirChoudhary 由于方法的productId 参数是原始int 类型,所以在比较完成之前product.getId() 将被解包为int
        【解决方案4】:

        您是否考虑过使用 HashMap(或 LinkedHashMap)而不是 Array。这样就可以把productId作为key,product作为value?

        这将使您无需遍历整个数组即可获取对象。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-08-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-12-10
          相关资源
          最近更新 更多