这个问题在这里已经有了答案:




9年前关闭。






请参阅此代码示例:

    List<String> list = new ArrayList<String>();
    list.add("hello");
    list.add("world");
    Object [] array = list.toArray();

为什么 toArray() 方法返回一个 Object 数组? Listget() 等其他 remove() 方法能够返回列表中对象的类型。

最佳答案

Because array has been in Java since the beginning, while generics
were only introduced in Java 5. And the List.toArray() method was
introduced in Java 1.2, before generics existed, so it was
specified to return Object[].

你可以使用 toArray(T[] a) 看看 Collection.toArray(T[] a)

关于java - 为什么 List.toArray() 返回一个 Object 数组?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11007998/

10-11 22:20
查看更多