数据拷贝
clone
int[] a1 = {1, 3};
int[] a2 = a1.clone();
System.arraycopy
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
Arrays.copyOf
int[] a1 = {1, 2, 3, 4, 5};
int[] a2 = Arrays.copyOf(a1, 3);
Arrays.copyOfRange
int[] a1 = {1, 2, 3, 4, 5};
int[] a2 = Arrays.copyOfRange(a1,0,4);
copyOfRange(int[] original, int from, int to)
数组转list
int[] 转 List
int[] data = {4, 5, 3, 6, 2, 5, 1};
List list1 = Arrays.stream(data).boxed().collect(Collectors.toList());
int[] 转 Integer[]
Integer[] integers1 = Arrays.stream(data).boxed().toArray(Integer[]::new);
list转数组
List 转 Integer[]
Integer[] array = list.toArray(new Integer[0]);
List 转 int[]
int[] arr1 = list1.stream().mapToInt(Integer::valueOf).toArray();
// 想要转换成int[]类型,就得先转成IntStream。
// 这里就通过mapToInt()把Stream<Integer>调用Integer::valueOf来转成IntStream
// 而IntStream中默认toArray()转成int[]。
Integer[] 转 int[]
int[] arr2 = Arrays.stream(integers1).mapToInt(Integer::valueOf).toArray();
// 思路同上。先将Integer[]转成Stream<Integer>,再转成IntStream。