ArrayList 题目1

2018-03-10  本文已影响14人  小熊先生很不开心

  ArrayList<Integer> list = new ArrayList<Integer>();在这个泛型为 Integer 的 ArrayList 中存放一个 String 类型的对象。

提示 : 泛型只有在编译过程中生效 运行时是没有效果的

package com.itheima.test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

public class Test2 {

    /**
        ArrayList<Integer> list = new ArrayList<Integer>();在这个泛型为 Integer 的 ArrayList
        中存放一个 String 类型的对象。
     * 
     */
    public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        ArrayList<Integer> list = new ArrayList<Integer>();
        Class clazz = ArrayList.class;
        Method me = clazz.getMethod("add", Object.class);
        me.invoke(list, "heima");
        System.out.println(list);
    }

}
上一篇 下一篇

猜你喜欢

热点阅读