/*如何利用反射,在Integer类型的List列表中插入String类型的数据*/
import java.lang.reflect.Method; import java.util.ArrayList;
import java.util.List;
public class FanXing{
public static void main(String[] args) throws Exception{
List<Integer> list = new ArrayList<Integer>();
list.add(1);
@SuppressWarnings("rawtypes")
Class c = list.getClass();
@SuppressWarnings("unchecked")
Method addMethod = c.getMethod("add", Object.class);
addMethod.invoke(list, "fsdfds");
System.out.println(list.get(1));
}
}
|