(利用ArrayList中的toArray函数):
ArrayList.toArray函数的返回值是object类型的数组,
因此首先需要对object类型数组进行强转成int类型数组, 方法如下:
//object[]转化为Integer[]类型
public int[] swap(Object[] object)
{
//第一步把object[]转化为Integer[]
Integer[]integers=new Integer[object.length];
integers=Arrays.copyOfRange(object,0,object.length,Integer[].class);
//第二步把Integer[]转化为Int类型(这个函数小编也不是很理解,大佬可以解释一下)
int[] num=Arrays.stream(integers).mapToInt(Integer::valueOf).toArray();
return num;
}
//ArrayList转化为Int[]
public static void main(String[] args) {
ArrayList<Integer> arrayList = new ArrayList<>();
int[]nums=swap(arrayList.toArray());
}
则nums[]为所求数组。
有其他更好的方法可以私聊我,小白别的方法也不会了........