如何让ArrayList添加一个String类型的元素呢,arrayliststring
分享于 点击 26052 次 点评:145
如何让ArrayList添加一个String类型的元素呢,arrayliststring
想必小伙伴们都很好奇吧,其实利用反射的机制就可以实现哦!让我们一起来看看哦:
public void integerListAddString(){
try {
ArrayList<Integer> integerArrayList = new ArrayList<>();
Method method = integerArrayList.getClass().getMethod("add", Object.class);
method.invoke(integerArrayList,"我是String");
System.out.println(integerArrayList.get(0));
}catch (Exception e){
e.printStackTrace();
}
}
输出结果
我是String
相关文章
- 暂无相关文章
用户点评