Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to compare Objects attributes in an ArrayList?
    primarykey
    data
    text
    <p>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? </p> <p>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, <code>Product ipad = new Product(12345, "iPad", 125.0, DeptCode.COMPUTER);</code> and <code>Product ipod = new Product(12356, "iPod", 125.0, DeptCode.ELECTRONICS);</code> (I have included this Product class below), and add them to an Arraylist such as, <code>List&lt;Product&gt; products = new ArrayList&lt;Product&gt;();</code> how can I loop through this ArrayList in order to find that product by its ID? This is the method I am working on: </p> <pre><code>List&lt;Product&gt; products = new ArrayList&lt;Product&gt;(); @Override public Product getProduct(int productId) { // TODO Auto-generated method stub for(int i=0; i&lt;products.size(); i++){ //if statement would go here //I was trying: if (Product.getId() == productId) { System.out.println(products.get(i)); } return null; }` </code></pre> <p>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? </p> <pre><code> 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; } } </code></pre> <p>Please let me know </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload