Java System.arraycopy()方法示例

Java System.arraycopy() is a native static method to copy elements from the source array to the destination array.

Java System.arraycopy()是一种本地静态方法,用于将元素从源数组复制到目标数组。

System.arraycopy()方法参数 (System.arraycopy() Method Arguments)

The System.arraycopy() method signature is:

System.arraycopy()方法签名为:

public static native void arraycopy(Object src,  int  srcPos,
     Object dest, int destPos,
     int length);
  • src: the source array.

    src :源数组。
  • srcPos: source array index from where copy will start

    srcPos :从其开始复制的源数组索引
  • dest: the destination array.

    dest :目标数组。
  • destPos: destination array index from where data will be copied.

    destPos :从中复制数据的目标数组索引。
  • length: the number of elements to copy.

    length :要复制的元素数。

Java System.arraycopy()示例 (Java System.arraycopy() Example)

Let’s look at a simple example to copy array using this method.

让我们看一个使用此方法复制数组的简单示例。

package com.journaldev.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));
	}
}

Output

输出量

Java System.arraycopy Example

Java System.arraycopy() Example

Java System.arraycopy()示例

The elements from the source array to copy are 4,5,6. These are copied to the destination array starting from index 2.

要复制的源数组中的元素为4,5,6。 这些将从索引2开始复制到目标数组。

当源和目标是同一数组时,System.arraycopy() (System.arraycopy() when source and destination are same array)

When the source and destination are the same arrays, a temporary array is created from the source array. Then the elements from the temporary array are copied to the source array.

当源和目标是相同的阵列时,将从源阵列创建一个临时阵列。 然后将临时数组中的元素复制到源数组。

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()方法的异常情况 (Exception Scenarios with System arraycopy() Method)

The arraycopy() method has 5 arguments. So, there are many instances when an exception can occur. The exceptions thrown by this method are:

arraycopy()方法有5个参数。 因此,在许多情况下会发生异常。 此方法引发的异常是:

  • NullPointerException: if source or destination array is null.

    NullPointerException :如果源或目标数组为null。
  • ArrayStoreException: if the source and destination array type doesn’t match or they are not array.

    ArrayStoreException :如果源和目标数组类型不匹配或不是数组。
  • ArrayIndexOutOfBoundsException: if the data overflow occurs because of index values or they are negative.

    ArrayIndexOutOfBoundsException :如果由于索引值导致数据溢出或它们为负数。

Let’s look at some examples of these exception scenarios.

让我们看一下这些异常情况的一些例子。

1. NullPointerException示例 (1. NullPointerException Examples)

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 (2. ArrayStoreException when source and destination are not an array)

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 (3. ArrayStoreException when source and destination arrays are not compatible)

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 (4. ArrayIndexOutOfBoundsException when index or length is negative)

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 (5. ArrayIndexOutOfBoundsException when srcPos+length is greater than src.length)

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 (6. ArrayIndexOutOfBoundsException when destPos+length is greater than dest.length)

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)

结论 (Conclusion)

Java System arraycopy() native method can be used to copy array elements. However, there are many scenarios which can lead to runtime exceptions. So you should use it carefully when you know the length of the source and destination array.

Java System arraycopy()本机方法可用于复制数组元素。 但是,有许多情况可能导致运行时异常。 因此,当您知道源数组和目标数组的长度时,应谨慎使用。

参考资料 (References)

翻译自: https://www.journaldev.com/32666/java-system-arraycopy-method-examples

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值