How to add an integer element to ArrayList of wildcards in Generics?

后端 未结 5 487
忘了有多久
忘了有多久 2021-01-17 00:44

I have a ArrayList generic wildcards type, which is taking Number as extends. I am trying to add the integer values to the ArrayList.

Buts it\'s giving me an error

5条回答
  •  天命终不由人
    2021-01-17 01:33

    You can't add a Integer to the list because maybe it is a list of Float, and not a list of Integer.

    You can do:

    ArrayList numberList = new ArrayList();
    numberList.add(100);
    

    or just

    ArrayList numberList = new ArrayList();
    numberList.add(100);
    

提交回复
热议问题