数组与列表之间转化
数组元素可以是基本类型或包装类,列表中只能存放包装类型的数据
以Integer为例,其他类型类似。
list->array(注:只能转化到包装类)
list.toArray方法
List<Integer> list =new ArrayList<>();
Integer[] array = list.toArray(new Integer[list.size()]);
array->list(注:只能用包装类转化)
Array.asList方法
Integer[] array = {1};
List<Integer> list = Arrays.asList(array);
数组基本类型和包装类之间转化
现在编程题中不能ArraysUtils类方法,所以也可以用for循环
包装类转为基本类型
Integer[] arrayRaw = {1};
int[] array = ArrayUtils.toPrimitive(arrayRaw);
//或者stream方法
int[] arrayBasic = Arrays.stream(arrayRaw).mapToInt(Integer::valueOf).toArray();
基本类型转为包装类
int[] basicArray = {1};
Integer[] array = ArrayUtils.toObject(basicArray);
//或者stream方法
Integer[] boxedArray = Arrays.stream(basicArray).boxed().toArray(Integer[]::new);