为何我在Iterator<Integer> 已经声明了是Integer,但是还是可以打印出来字符"abc"呢?
- package exercise;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.util.ArrayList;
- import java.util.Iterator;
- /**
- * ArrayList<Integer> list = new ArrayList<Integer>();
- * 在这个泛型为Integer的ArrayList中存放一个String类型的对象。
- * @author feaoes
- *
- */
- public class Test3 {
- public static void main(String[] args) {
- ArrayList<Integer> list = new ArrayList<Integer>();
- list.add(1);
- list.add(2);
- for(Integer it:list)
- {
- System.out.println(it);
- }
-
-
- try {
- Method clazzMethod=ArrayList.class.getMethod("add", Object.class);
- clazzMethod.invoke(list, "abc");
- Iterator<Integer> it=list.iterator();
- while(it.hasNext())
- {
- System.out.println(it.next());
- }
- } catch (NoSuchMethodException e) {
- e.printStackTrace();
- } catch (SecurityException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- }
-
-
-
-
- }
- }
复制代码 |