The Java array declaration must declare its length, but what should we do when we want to amplify the arrays?
The following three ways can be amplified, the last of which is also the most convenient.
1 /**2 * Manual Cycle Amplification3 */4 int[] arr = {1, 2, 3 };5 int[] Temparray =New int[Arr.length * 2];6 for(inti = 0; i < arr.length; i++) {7Temparray[i] =Arr[i];8 }9 //Temparray = {1, 2, 3, 0, 0, 0}Ten One /** A * System.arraycopy - * Copy the original array into a new long-length array to achieve the purpose of amplification - */ the int[] arr1 = {1, 2, 3 }; - int[] NewArray =New int[Arr1.length * 2]; -System.arraycopy (arr1, 0, NewArray, 0, arr.length); - //NewArray = {1, 2, 3, 0, 0, 0} + - /** + * arrays.copyof A * The underlying implementation or System.arraycopy at * Amplification of itself - */ - int[] arr2 = {1, 2, 3 }; -ARR2 = arrays.copyof (arr2, Arr2.length * 2); - //arr2 = {1, 2, 3, 0, 0, 0}
Three ways to expand the Java array