Error: the method getId() is undefined for the type List

后端 未结 4 1859
温柔的废话
温柔的废话 2021-01-24 08:51

I have a method to create a list of objects of class

public List initProducts(){
    List product = new ArrayList();         


        
相关标签:
4条回答
  • 2021-01-24 09:28

    Is my statement correct??

    Product prod = new Product(product.getId(),product.getItemName(), product.getPrice(),  
    product.getCount());
    product.add(prod);
    

    NO this is incorrect. product is not an instance of class Product,rather it is an instance of List. List does not have any method called getId.

    If you want to retrieve the elements from the list and use it to create another instance of you can do something like:

    Product exisProd = product.get(0);
    Product prod = new Product(exisProd .getId(),exisProd .getItemName(), exisProd .getPrice(),  
        exisProd .getCount());
    

    But make sure that you have elements in the list, otherwise u may run into exception. product.add(prod);

    0 讨论(0)
  • 2021-01-24 09:29

    I believe, the reason you are facing this issue is more due not following the code conventions, that any other.

    Whenever you make a collection of any objects, the convention is to use plurals for reference names of the collection. And singular reference name of the Object itself. You can find more details here.

    Below is the re-written code with the code conventions being followed:

    Method to create a list of objects of class Product:

    public List<Product> initProducts(){
        List<Product> products = new ArrayList<Product>();
        Product product = new Product(products.getId(), products.getItemName(), products.getPrice(), products.getCount());
        products.add(prod);    
    }
    

    Product Class:

    class Product {
    
    int itemCode;
    String itemName;
    double unitPrice;
    int count;
    
    public Product(int itemCode, String itemName, double unitPrice, int count)
    {
        this.itemCode = itemCode;
        this.itemName = itemName;
        this.unitPrice = unitPrice;
        this.count = count;
    }
    
    public int getId()
    {
       return this.itemCode;
    }
    
    public String getItemName()
    {
       return this.itemName;
    }
    
    public double getPrice()
    {
       return this.unitPrice;
    }
    
    public int getCount()
    {
       return this.count;
    }
    }
    

    Now, it is easy to see, that the products Object (which is of the type List) will not have any methods name getId() or getCount(). Infact , these are methods of the Object contained in the List.

    Following conventions will help you avoid, such hassles in futures.

    0 讨论(0)
  • 2021-01-24 09:34

    product is reference to List object.

    and List/ArrayList has no methosd named getId().

    You have written getId() method for Prodct class , so you can call this method using ref to Product class object.

    If you want to get any product object form list use get(int index) method of ArrayList.

    eg.

    Product prod = product.get(0);
    String id= prod.getId();
    
    0 讨论(0)
  • 2021-01-24 09:41

    product is a reference of List

    List<Product> product = new ArrayList<Product>();
    

    which doesn't have that method

    0 讨论(0)
提交回复
热议问题