Java List toArray(T[] a) implementation

后端 未结 5 1337
挽巷
挽巷 2021-01-04 13:26

I was just looking at the method defined in the List interface:

Returns an array containing all of the elements in this list in the correct order; the

5条回答
  •  攒了一身酷
    2021-01-04 13:28

    As mentioned by others, there are a couple different reasons:

    • You need to pass in the type somehow, and passing in an array of the specified type isn't an unreasonable way to do it. Admittedly, it might be nice if there was a version that you pass in the Class of the type you want too, for speed.
    • If you want to reuse your array, you can keep passing in the same one, rather than needing to create a new one each time. This can save time and memory, and GC issues if you need to do it many, many times

提交回复
热议问题