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

后端 未结 4 1858
温柔的废话
温柔的废话 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: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();
    

提交回复
热议问题