扩容数组

package chapter7;
//数组扩容

public class TestArray {
public static void main(String[] args) {

// 创建数组
int[] a = { 1, 4, 8, 6, 9 };
// 扩容数组
int[] b = new int[a.length];
// 复制旧数组到新数组
for (int i = 0; i < a.length; i++) {
// a数组复制给b数组
b[i] = a[i];

}

// a = b;//多余
// 调用方法
print(a);
}

// 遍历数组,定义一个形参作为局部变量
public static void print(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}

猜你喜欢

转载自www.cnblogs.com/Koma-vv/p/9542047.html