System.arraycopy
1
import java.util.Arrays;
2
/**
3
* 老紫竹JAVA提高教程 - System.arraycopy方法的使用。<br>
4
* <br>
5
* 从指定源数组中复制一个数组,复制从指定的位置开始,<br>
6
* 到目标数组的指定位置结束
7
*
8
* @author 老紫竹的家(java2000.net,laozizhu.com)
9
*
10
*/
11
public class LessionSystemArraycopy {
12
public static void main(String[] args) {
13
// 此方位为native方法。
14
// public static native void arraycopy(
15
// Object src, int srcPos, Object dest,
16
// int destPos, int length);
17
// 初始化
18
int[] ids = { 1, 2, 3, 4, 5 };
19
System.out.println(Arrays.toString(ids)); // [1, 2, 3, 4, 5]
20
// 测试自我复制
21
// 把从索引0开始的2个数字复制到索引为3的位置上
22
System.arraycopy(ids, 0, ids, 3, 2);
23
System.out.println(Arrays.toString(ids)); // [1, 2, 3, 1, 2]
24
// 测试复制到别的数组上
25
// 将数据的索引1开始的3个数据复制到目标的索引为0的位置上
26
int[] ids2 = new int[6];
27
System.arraycopy(ids, 1, ids2, 0, 3);
28
System.out.println(Arrays.toString(ids2)); // [2, 3, 1, 0, 0, 0]
29
// 此功能要求
30
// 源的起始位置+长度不能超过末尾
31
// 目标起始位置+长度不能超过末尾
32
// 且所有的参数不能为负数
33
try {
34
System.arraycopy(ids, 0, ids2, 0, ids.length + 1);
35
} catch (IndexOutOfBoundsException ex) {
36
// 发生越界异常,数据不会改变
37
System.out.println("拷贝发生异常:数据越界。");
38
}
39
System.out.println(Arrays.toString(ids2)); // [2, 3, 1, 0, 0, 0]
40
// 如果是类型转换问题
41
Object[] o1 = { 1, 2, 3, 4.5, 6.7 };
42
Integer[] o2 = new Integer[5];
43
System.out.println(Arrays.toString(o2)); // [null, null, null, null, null]
44
try {
45
System.arraycopy(o1, 0, o2, 0, o1.length);
46
} catch (ArrayStoreException ex) {
47
// 发生存储转换,部分成功的数据会被复制过去
48
System.out.println("拷贝发生异常:数据转换错误,无法存储。");
49
}
50
// 从结果看,前面3个可以复制的数据已经被存储了。剩下的则没有
51
System.out.println(Arrays.toString(o2)); // [1, 2, 3, null, null]
52
}
53
}
54
55
56
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/java2000_net/archive/2009/04/09/4059465.aspx
import java.util.Arrays; 2
/** 3
* 老紫竹JAVA提高教程 - System.arraycopy方法的使用。<br> 4
* <br> 5
* 从指定源数组中复制一个数组,复制从指定的位置开始,<br> 6
* 到目标数组的指定位置结束 7
* 8
* @author 老紫竹的家(java2000.net,laozizhu.com) 9
* 10
*/ 11
public class LessionSystemArraycopy { 12
public static void main(String[] args) { 13
// 此方位为native方法。 14
// public static native void arraycopy( 15
// Object src, int srcPos, Object dest, 16
// int destPos, int length); 17
// 初始化 18
int[] ids = { 1, 2, 3, 4, 5 }; 19
System.out.println(Arrays.toString(ids)); // [1, 2, 3, 4, 5] 20
// 测试自我复制 21
// 把从索引0开始的2个数字复制到索引为3的位置上 22
System.arraycopy(ids, 0, ids, 3, 2); 23
System.out.println(Arrays.toString(ids)); // [1, 2, 3, 1, 2] 24
// 测试复制到别的数组上 25
// 将数据的索引1开始的3个数据复制到目标的索引为0的位置上 26
int[] ids2 = new int[6]; 27
System.arraycopy(ids, 1, ids2, 0, 3); 28
System.out.println(Arrays.toString(ids2)); // [2, 3, 1, 0, 0, 0] 29
// 此功能要求 30
// 源的起始位置+长度不能超过末尾 31
// 目标起始位置+长度不能超过末尾 32
// 且所有的参数不能为负数 33
try { 34
System.arraycopy(ids, 0, ids2, 0, ids.length + 1); 35
} catch (IndexOutOfBoundsException ex) { 36
// 发生越界异常,数据不会改变 37
System.out.println("拷贝发生异常:数据越界。"); 38
} 39
System.out.println(Arrays.toString(ids2)); // [2, 3, 1, 0, 0, 0] 40
// 如果是类型转换问题 41
Object[] o1 = { 1, 2, 3, 4.5, 6.7 }; 42
Integer[] o2 = new Integer[5]; 43
System.out.println(Arrays.toString(o2)); // [null, null, null, null, null] 44
try { 45
System.arraycopy(o1, 0, o2, 0, o1.length); 46
} catch (ArrayStoreException ex) { 47
// 发生存储转换,部分成功的数据会被复制过去 48
System.out.println("拷贝发生异常:数据转换错误,无法存储。"); 49
} 50
// 从结果看,前面3个可以复制的数据已经被存储了。剩下的则没有 51
System.out.println(Arrays.toString(o2)); // [1, 2, 3, null, null] 52
} 53
} 54

55

56
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/java2000_net/archive/2009/04/09/4059465.aspx


浙公网安备 33010602011771号