| public class insertStrToArray {
 public static void main(String[] args) throws Exception {
 ArrayList<Integer> list = new ArrayList<Integer>();
 Method methodAddString = list.getClass().getMethod("add", Object.class);
 methodAddString.invoke(list, "abc");
 System.out.println(list);
 }
 }
 这个可以通过反射的方式实现,因为泛型的作用层在编译时。而反射直接获得了add方法的字节码跳过编译层在运行时直接添加。这样就骗过了编译。
 作用嘛,比如你得这个集合里存放int类型的数据,但是有及个别的地方需要添加其他类型的数据。就可以用上了!
 |