How can I convert ArrayList<Object> to ArrayList?

后端 未结 11 810
-上瘾入骨i
-上瘾入骨i 2020-11-28 06:31
ArrayList list = new ArrayList();
list.add(1);
list.add(\"Java\");
list.add(3.14);
System.out.println(list.toString());


11条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-28 07:17

    It's not safe to do that!
    Imagine if you had:

    ArrayList list = new ArrayList();
    list.add(new Employee("Jonh"));
    list.add(new Car("BMW","M3"));
    list.add(new Chocolate("Twix"));
    
    
    

    It wouldn't make sense to convert the list of those Objects to any type.

    提交回复
    热议问题