绕过泛型,通过反射把 String 添加到 List<Integer> 中
2016-09-06 15:31
651 查看
public class ArrayListDemo { public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { List<Integer> list = new ArrayList<>(); // list.add(1); // list.add(2); // list.add("hello"); Class c = list.getClass(); Method method = c.getMethod("add",Object.class); method.invoke(list,"hello"); method.invoke(list,"world"); method.invoke(list,"liwei"); System.out.println(list); } }
相关文章推荐
- 通过反射绕过编译向泛型为String的ArrayList里面添加整型
- 通过反射把list<T>转换成datatable,kill进程,事务传入一个Lst<String>多条sql语句
- ArrayList<Integer> list = new ArrayList<Integer>(); 在这个泛型为Integer的ArrayList中存放一个String类型的对象。
- 【java】反射(二)——泛型集合(在ArrayList<Integer>中加入String对象)
- ArrayList<Integer> list = new ArrayList<Integer>();在这个泛型为 Integer 的 ArrayList 中存放一个 String 类型的对象。
- 黑马程序员 :(反射应用 )通过反射讲<Integer>类型的集合中添加一个<String>类型的数据
- Java 面试题之反射的应用(如何在List<String> list = new ArrayList<String>();中放入Integer类型的数据)
- C#中string[]数组和list<string>泛型的相互转换
- 一个Thiking泛型的例子,突然发现理解了List&lt;String&gt;的原理
- Java 中的 int 与 Integer 用于 List<Integer> 时,以及通过打印变量检測程序执行和函数调用次数计数
- 通过Struts标签和JSTL标签来遍历List<Map<String,String>>格式的数据
- 两个数组分出重复和不重复数据通过Map<String,Integer>集合来处理
- C#中string[]数组和list<string>泛型的相互转换
- C#中string[]数组和list<string>泛型的相互转换
- .NET中string[]数组和List<string>泛型的相互转换
- .NET中string[]数组和List<string>泛型的相互转换以及Array类的Sort()方法(转)
- C#中string[]数组和list<string>泛型的相互转换
- C#中string[]数组和list<string>泛型的相互转换 【转】
- 反射遍历List<>泛型类型
- C# List<T> 泛型的反射, 泛型 函数的反射