jdk提供的数组扩容方法:System.arraycopy

package chapter7;

/*
* jdk提供的扩容方法
* System.arraycopy
*/
public class TestArrayjdk {
public static void main(String[] args) {
// 定义数组
int[] a = { 2, 8, 6, 5, 3 };
// 定义一个扩容数组
int[] b = new int[a.length];
// 用jdk提供的方法存储数组
/*
* 第一个参数:Object src: 原数组---a
* 第二个参数:int srcPos: 从原数组中的哪个下标位置开始拷贝
* 第三个参数:Object dest: 目标数组---b
* 第四个参数:int destPos:从目标数组的那个下标位置开始放
* 第五个参数:int length:拷贝的元素的个数
*/
System.arraycopy(a, 0, b, 0, a.length);
//
// a = b;//可以省略该步骤
print(a);
}

public static void print(int array[]) {
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}

posted @ 2018-08-27 15:24  Monica_维维  阅读(305)  评论(0编辑  收藏  举报