Type mismatch: cannot convert from element type Object to Parent

后端 未结 2 1903
轮回少年
轮回少年 2021-01-29 10:03

i\'m trying to develop an e4 application but i have an error : in this part \"error:Type mismatch: cannot convert from element type Object to Parent\" any help please thanks in

相关标签:
2条回答
  • 2021-01-29 10:31

    You are just using the 'raw type' List for your list so Java does not know that this is a list of Parent objects and can only treat it as a list of Object.

    You need to use generics to specify the list type - everywhere you have List it should be List<Parent>.

    So something like:

    public class ParentsHolder extends Model {
        List<Parent> parents = new ArrayList<>();
    
        public List<Parent> getParents() {
            return parents;
        }
    
        public void setParents(List<Parent> parents) {
            firePropertyChange("parents", this.parents, this.parents = parents);
        }
    
        public void addParent(Parent p) {
            List<Parent> newlist = new ArrayList<>(parents);
            newlist.add(p);
            setParents(newlist);
        }
    }
    
    0 讨论(0)
  • 2021-01-29 10:32

    change corresponding lines to below code as:

    for(Object p : parentholder.getParents())
          {
              Parent p1= (Parent)p;
               //your code .....
            }
    

    similarily for Child class

     for(Object p : parentholder.getParents())
          {
              Child child= (Parent)p;
               //your code .....
            }
    
    0 讨论(0)
提交回复
热议问题