Unable to add object to generic list using wildcard [duplicate]

人盡茶涼 提交于 2020-01-07 01:21:11

问题


Why can't I add any object here , I was hoping I would be allowed to add any kind of object in such a list.

List<?> l = new ArrayList<Object>();
l.add(new Object());
l.add(new String("hello"));

回答1:


Why not just:

    List<Object> l = new ArrayList<>();
    l.add(new Object());
    l.add(new String("hello"));


来源:https://stackoverflow.com/questions/24756464/unable-to-add-object-to-generic-list-using-wildcard

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!