怎么获取一个泛型List的元素类型
问题描述:
如题,有一个泛型 List 不为空,但是长度为0,怎么获取到这个List中的元素类型T是什么?
答
List list = new ArrayList(0);
Method method = list.class.getMethod("get",null);
Class returnTypeClass = method.getReturnType();
returnTypeClass就是返回类型了
答
list.get(0).getClass()
假设list为List的引用,并且长度不为空