2013-08-23 32 views
0

我对Java相当陌生,而且我已经耗尽了所有当前资源以找到答案。我想知道是否有可能访问一个对象的第一个属性,看看它是否匹配一个特定的整数?如何比较ArrayList中的对象属性?

例如,我试图通过按产品ID搜索产品来获得位于我的数据库中的产品。因此,如果我创建了两个产品,例如Product ipad = new Product(12345, "iPad", 125.0, DeptCode.COMPUTER);Product ipod = new Product(12356, "iPod", 125.0, DeptCode.ELECTRONICS);(我已在下面包括此产品类别),并将它们添加到诸如List<Product> products = new ArrayList<Product>();之类的Arraylist,如何通过此ArrayList循环以便通过其ID找到该产品? 这是我的工作方法:我知道我可以包括在for循环条件语句,但我无法弄清楚如何访问getId()方法在产品类比较它

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

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

} 

请让我知道

回答

4

您已经在下面的语句得到了ProductList<Product>的:现在

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

,你已经得到了Product,现在要得到它的编号,你可以把它叫做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; 
    } 
} 

此外,你应该从Integer改变的productId类型int。你不需要包装类型。

+0

==比较的引用不上的对象的值和整数是一个对象 –

+0

@YasirShabbirChoudhary由于'productId'参数的方法是一种原始'int'类型,'product.getId()'将被解包,以在比较完成之前'int'。 –

0

根据你的评论专栏//I was trying: if (Product.getId() == productId)我认为你在哪里倒下正在使用Product(大写字母P)。你需要的是:

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

而且,你不回来你发现产品......

与形式的问题是,一个),你必须在列表中找到产品两次(在条件中一次,然后在打印结果时再次 - 如果您返回产品,则是第三次),以及b)如果您正在查找的产品不在列表中,它将抛出空指针异常。

这是做它的一个更好的,更紧凑的方式:

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

你有没有使用一个HashMap(或者LinkedHashMap)考虑,而不是一个数组。这样,您可以使用productId作为关键字,并将产品用作值?

这将让你获得对象而不必循环整个数组。

0

用于比较ArrayList对象在您的CustomObject类Like 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类,并且在该类中您需要重写等价函数。

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



} 

我希望这个解决方案将帮助您检查是否相等值和比较值。

由于