How to create array of generic type?

后端 未结 2 601
心在旅途
心在旅途 2021-01-22 05:06

How to create array of generic type? How does generic Arrays.copyOf() method work? It returns a copy of generic array. So it is possible to create generic arrays. B

2条回答
  •  逝去的感伤
    2021-01-22 06:07

    If you need to create it at runtime you need at least know the type at that very moment, so you could use the following approach:

    a = (T[]) Array.newInstance(c,s);
    

    where T is the generic type, c is the class of T and s is the initial size.

    Documentation here

提交回复
热议问题