数组扩容的3种方式
import java.util.Arrays;
/**
* およそ神
* 数组扩容
*/
public class kuo {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
//创建新数组扩容使用循环
int[] arr1 = new int[arr.length * 2];
for (int i = 0; i < arr.length; i++) {
arr1[i] = arr[i];
}
arr = arr1;
System.out.println(Arrays.toString(arr));
//调用sestem.arraycopy方法扩容
System.arraycopy(arr,0,arr1,0,arr.length);
arr = arr1;
System.out.println(Arrays.toString(arr));
//调用Arrays.copyOf方法
int arr2[] = Arrays.copyOf(arr, arr.length * 2);
arr = arr2;
System.out.println(Arrays.toString(arr));
}
}