Java System.arraycopy()方法示例
时间:2020-02-23 14:36:56 来源:igfitidea点击:
Java System.arraycopy()是一种本地静态方法,用于将元素从源数组复制到目标数组。
System.arraycopy()方法参数
System.arraycopy()方法签名为:
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
src:源数组。
srcPos:从中开始复制的源数组索引
dest:目标数组。
destPos:从中复制数据的目标数组索引。
长度:要复制的元素数。
Java System.arraycopy()示例
让我们看一个使用此方法复制数组的简单示例。
package com.theitroad.examples; import java.util.Arrays; public class SystemArrayCopy { public static void main(String[] args) { int[] source = { 1, 2, 3, 4, 5, 6, 7 }; int[] destination = new int[5]; System.arraycopy(source, 3, destination, 2, 3); System.out.println(Arrays.toString(destination)); } }
Java System.arraycopy()示例
要复制的源数组中的元素为4,5,6。
它们将从索引2开始复制到目标数组。
当源和目标是同一数组时,System.arraycopy()
当源和目标是相同的阵列时,将从源阵列创建一个临时阵列。
然后将临时数组中的元素复制到源数组。
jshell> int[] source = { 1, 2, 3, 4, 5, 6, 7 }; source ==> int[7] { 1, 2, 3, 4, 5, 6, 7 } jshell> System.arraycopy(source, 3, source, 5, 2); jshell> System.out.println(Arrays.toString(source)); [1, 2, 3, 4, 5, 4, 5]
系统arraycopy()方法的异常情况
arraycopy()方法有5个参数。
因此,在许多情况下会发生异常。
此方法引发的异常是:
NullPointerException:如果源或者目标数组为null。
ArrayStoreException:如果源和目标数组类型不匹配或者不是数组。
ArrayIndexOutOfBoundsException:如果由于索引值导致数据溢出或者它们为负数。
让我们看一下这些异常情况的一些示例。
1. NullPointerException示例
jshell> System.arraycopy(source, 3, null, 2, 3); | Exception java.lang.NullPointerException | at System.arraycopy (Native Method) | at (#4:1) jshell> System.arraycopy(null, 3, source, 2, 3); | Exception java.lang.NullPointerException | at System.arraycopy (Native Method) | at (#5:1)
2.当源和目标不是数组时,ArrayStoreException
jshell> System.arraycopy(new Object(), 3, source, 2, 3); | Exception java.lang.ArrayStoreException: arraycopy: source type java.lang.Object is not an array | at System.arraycopy (Native Method) | at (#6:1) jshell> System.arraycopy(source, 3, "ABC", 2, 3); | Exception java.lang.ArrayStoreException: arraycopy: destination type java.lang.String is not an array | at System.arraycopy (Native Method) | at (#7:1)
3.当源和目标数组不兼容时,ArrayStoreException
jshell> int[] source = {1, 2, 3} source ==> int[3] { 1, 2, 3 } jshell> String[] destination = new String[5]; destination ==> String[5] { null, null, null, null, null } jshell> System.arraycopy(source, 0, destination, 0, 2); | Exception java.lang.ArrayStoreException: arraycopy: type mismatch: can not copy int[] into object array[] | at System.arraycopy (Native Method) | at (#10:1)
4.索引或者长度为负数时,ArrayIndexOutOfBoundsException
jshell> int[] source = {1, 2, 3} source ==> int[3] { 1, 2, 3 } jshell> int[] dest = new int[5]; dest ==> int[5] { 0, 0, 0, 0, 0 } jshell> System.arraycopy(source, -1, dest, 0, 2); | Exception java.lang.ArrayIndexOutOfBoundsException: arraycopy: source index -1 out of bounds for int[3] | at System.arraycopy (Native Method) | at (#15:1) jshell> System.arraycopy(source, 1, dest, -1, 2); | Exception java.lang.ArrayIndexOutOfBoundsException: arraycopy: destination index -1 out of bounds for int[5] | at System.arraycopy (Native Method) | at (#16:1) jshell> System.arraycopy(source, 1, dest, 0, -2); | Exception java.lang.ArrayIndexOutOfBoundsException: arraycopy: length -2 is negative | at System.arraycopy (Native Method) | at (#17:1)
5. srcPos + length大于src.length时,ArrayIndexOutOfBoundsException
jshell> System.arraycopy(source, 0, dest, 0, 4); | Exception java.lang.ArrayIndexOutOfBoundsException: arraycopy: last source index 4 out of bounds for int[3] | at System.arraycopy (Native Method) | at (#18:1)
6.当destPos + length大于dest.length时,ArrayIndexOutOfBoundsException
jshell> int[] src = {1, 2, 3, 4} src ==> int[4] { 1, 2, 3, 4 } jshell> int[] dest = new int[2] dest ==> int[2] { 0, 0 } jshell> System.arraycopy(src, 0, dest, 0, 3); | Exception java.lang.ArrayIndexOutOfBoundsException: arraycopy: last destination index 3 out of bounds for int[2] | at System.arraycopy (Native Method) | at (#21:1)