这里利用了Array.newInstance(..)方法,但还有一个问题,当List的长度为0的时候,是获得不了T的具体class的。好在长度为0的数组没有意义,所以干脆返回了null。

代码如下:

    public static <T> T[] getArray(List<T> collection) {
if (collection == null || collection.size() == 0)
return null;
@SuppressWarnings("unchecked")
T[] tArray = ((T[]) Array.newInstance(collection.get(0).getClass(), 0));
return collection.toArray(tArray);
}



相关文章:

  • 2022-12-23
  • 2022-02-08
  • 2022-12-23
  • 2021-07-20
  • 2021-09-02
  • 2021-11-22
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-08
  • 2021-10-10
  • 2021-12-14
  • 2022-01-22
  • 2022-02-18
  • 2021-08-10
  • 2021-06-26
相关资源
相似解决方案