存入int类型后 for循环不能了,该怎么遍历出来呢?没讲啊
Method method = list.getClass().getMethod("add", Object.class);
Object invoke = method.invoke(list, 2323);
Method method = list.getClass().getMethod("add", Object.class);
Object invoke = method.invoke(list, 2323);
2019-06-19
package reflect.analytical.generics;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class TestClass {
public static void main(String[] args) {
List list1 = new ArrayList();
Class class1 = list1.getClass();
List<String> list3 = new ArrayList<String>();
list3.add("java");
Class class3 = list3.getClass();
System.out.println(class1 == class3);
/*反射是编译后执行的;
*集合中的泛型用于防止错误输入, 只在编译阶段有效。
* */
try {
Method method = class3.getMethod("add", Object.class);
method.invoke(list3, 20000);
System.out.println("list3.size()" + list3.size());
Iterator it = list3.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//执行结果:
true
list3.size()2
java
20000
举报