Creating an array of generic collections

前端 未结 3 577
情话喂你
情话喂你 2021-02-07 03:20

Actually, the question should be

Creating an array of generic anything.

Why can\'t the compiler take care of it?

The following would be

3条回答
  •  我在风中等你
    2021-02-07 03:45

    I do know that, relative to the workarounds to this issue, Array.newInstance() is an expensive method to call. IIRC it uses a native method to instantiate the array, amidst the other reflection involved. I can't offer any statistics, but this seems like a good enough reason for such functionality not to be automatically substituted in by the compiler in order to allow generic array creation. Especially given the existence of ArrayList, etc. it just doesn't seem like a pressing issue.

提交回复
热议问题