import java.util.Arrays;
public class arrayDilatation {
public static void main(String[] args) {
dilatation1();
dilatation2();
dilatation3();
}
private static void dilatation3() {
int [] arr = {1,2,3,4};
arr = Arrays.copyOf(arr,arr.length*2);
System.out.println(Arrays.toString(arr));
}
private static void dilatation2() {
int [] arr = {1,2,3,4};
int [] arr1 = new int[arr.length*2];
System.arraycopy(arr,0,arr1,0,arr.length);
arr = arr1;
System.out.println(Arrays.toString(arr));
}
private static void dilatation1() {
int [] num = {1,2,3,4};
int [] mun = new int[num.length*2];
for (int i = 0; i < num.length; i++) {
mun[i] = num [i];
}
num = mun;
System.out.println(Arrays.toString(num));
}
}
Java------数组的扩容
最新推荐文章于 2024-08-15 14:47:41 发布