a- import java.lang.reflect.Method;
- import java.util.ArrayList;
- public class T9 {
- /**
- * 向泛型为Integer的ArrayList中添加一个String类型元素
- * @throws Exception
- * @throws NoSuchMethodException
- */
- public static void main(String[] args) throws NoSuchMethodException, Exception {
- ArrayList<Integer> al = new ArrayList<Integer>();
- Class cl = al.getClass();
- Method add = cl.getMethod("add", Object.class);
- add.invoke(al, "hell java");
- System.out.println(al);
- }
- }
复制代码 |
|