首先我在网上看到了这样一段代码
ArrayList<String> list=new ArrayList<String>();
String[] strings = new String[list.size()];
list.toArray(strings);
于是我照猫画虎写了这样一段经典错误代码,错误代码就不贴了直接截图吧
错误原因是这样的
很简单的原因
int不是一个对象类,改成Integer就好了
List<Integer> te = new ArrayList<>();
Integer [] re = te.toArray(new Integer[te.size()]);
那么我们的问题还是没有解决,没办法只能循环解决了
不过这里补充一下,虽然int不是对象,但是int型的数组是对象,例子如下
List<int[]> intevals=new ArrayList<>();
int[][] re = intevals.toArray(new int[intevals.size()][]);
int[]是可以写进泛型里面的。