Java: How can I dynamically create an array of a specified type based on the type of an object?

后端 未结 4 1194
借酒劲吻你
借酒劲吻你 2020-12-10 03:47

I would like to take a passed List that I know is homogeneous and from it create an array of the same type as the elements within it.

Something like...



        
4条回答
  •  佛祖请我去吃肉
    2020-12-10 04:42

    If you need to dynamically create an array based on a type known only at runtime (say you're doing reflection or generics) you'll probably want Array.newInstance

提交回复
热议问题