Why Collection.toArray(T[]) doesn't take an E[] instead

前端 未结 2 1770
说谎
说谎 2021-01-04 12:54

The toArray method (lets pick the implementation in java.util.ArrayList) is the following:

class ArrayList ....{
    public  T         


        
2条回答
  •  孤城傲影
    2021-01-04 13:46

    The point of the is if the array desired is of a base class of E. For example if E is HashMap but the desired array was Map[]. If toArray were locked down to E this would not be possible.

    This type of thing is not needed in generic collections / types due to type-erasure. But there is no type-erasure with arrays so the type of the array can be very important.

提交回复
热议问题