如何向ArrayList<Integer> 中写入一个字符串
利用反射实现
package cn.haoju.reflections; import java.lang.reflect.Method; import java.util.ArrayList; public class ReflectionDemo { public static void main(String[] args) throws Exception { ArrayList<Integer> arrayList = new ArrayList<Integer>(); arrayList.add(3); //arrayList.add("lihao"); //编译不通过 Class listClass = arrayList.getClass(); Method method = listClass.getDeclaredMethod("add", Object.class); method.invoke(arrayList, "lisan"); System.out.println(arrayList); //[3, lisan] } }