侧边栏壁纸
博主头像
小顺

一帆风顺 ⛵️⛵️⛵️

  • 累计撰写 64 篇文章
  • 累计创建 0 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

绕过泛型,通过反射把 String 添加到 List < Integer > 中

小顺
2021-05-25 / 0 评论 / 0 点赞 / 36 阅读 / 58 字

public class ArrayListDemo {

public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    List<Integer> list = new ArrayList<Integer>();

    list.add(1);
    // list.add("hello");不能添加
  
    Class clzz = list.getClass();
    Method method = clzz.getMethod("add",Object.class);
    method.invoke(list,"hello");

    System.out.println(list);
}

}

0

评论区