Field field = TestList.class.getDeclaredField("list"); //myList的类型是List
Type type = field.getGenericType();
if (type instanceof ParameterizedType) {
ParameterizedType paramType = (ParameterizedType) type;
Type[] actualTypes = paramType.getActualTypeArguments();
for (Type aType : actualTypes) {
if (aType instanceof Class) {
Class clz = (Class) aType;
System.out.println(clz.getName()); //输出java.lang.String
}
}
}
Type type = field.getGenericType();
if (type instanceof ParameterizedType) {
ParameterizedType paramType = (ParameterizedType) type;
Type[] actualTypes = paramType.getActualTypeArguments();
for (Type aType : actualTypes) {
if (aType instanceof Class) {
Class clz = (Class) aType;
System.out.println(clz.getName()); //输出java.lang.String
}
}
}
Java反射获取List类型的元素
本文详细介绍了如何使用Java反射API获取对象中的List类型的元素,包括获取类型、判断实际类型和打印元素名称。
1253

被折叠的 条评论
为什么被折叠?



