黑马入学测试题:
ArrayList list = new ArrayList();
在这个泛型为Integer的ArrayList中存放一个String类型的对象。
package itheima;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
/**
* 在这个泛型为Integer的ArrayList中存放一个String类型的对象。
* @author Administrator
*
*/
public class AddStringToArrayList {
//用反射机制
public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
ArrayList<Integer> list=new ArrayList<Integer>();
Method method = list.getClass().getMethod("add", Object.class);
method.invoke(list, "i am a String");
System.out.println(list);
}
}