1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
/*
* System类方法,复制数组
* arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
* Object src, 要复制的源数组
* int srcPos, 数组源的起始索引
* Object dest,复制后的目标数组
* int destPos,目标数组起始索引
* int length, 复制几个
*/
public static void function(){
String[] olds= {"j","d"};
String[] news= {"h","a","i","",""}; //因为下面的拷贝中,我们从索引3的位置拷贝了两个元素,所以总共有5个元素,要注意拷贝后的数组的长度,否则会发生数组越界的异常
System.arraycopy(olds, 0, news, 3, 2);
for (String n : news) {
System.out.println(n); //输出:h a i j d
}
}
|