Java使用System.arraycopy复制数组

原创 2016年05月31日 17:48:01

我们在项目中经常会复制一个数组元素到另一个数组里面,java复制数组的方法有很多,在这里介绍下JDK自带的System.arraycopy方法,这个方法使用想当简单快捷。

    /**
     * Copies an array from the specified source array, beginning at the
     * specified position, to the specified position of the destination array.
     * A subsequence of array components are copied from the source
     * array referenced by <code>src</code> to the destination array
     * referenced by <code>dest</code>. The number of components copied is
     * equal to the <code>length</code> argument. The components at
     * positions <code>srcPos</code> through
     * <code>srcPos+length-1</code> in the source array are copied into
     * positions <code>destPos</code> through
     * <code>destPos+length-1</code>, respectively, of the destination
     * array.
     * <p>
     * If the <code>src</code> and <code>dest</code> arguments refer to the
     * same array object, then the copying is performed as if the
     * components at positions <code>srcPos</code> through
     * <code>srcPos+length-1</code> were first copied to a temporary
     * array with <code>length</code> components and then the contents of
     * the temporary array were copied into positions
     * <code>destPos</code> through <code>destPos+length-1</code> of the
     * destination array.
     * <p>
     * If <code>dest</code> is <code>null</code>, then a
     * <code>NullPointerException</code> is thrown.
     * <p>
     * If <code>src</code> is <code>null</code>, then a
     * <code>NullPointerException</code> is thrown and the destination
     * array is not modified.
     * <p>
     * Otherwise, if any of the following is true, an
     * <code>ArrayStoreException</code> is thrown and the destination is
     * not modified:
     * <ul>
     * <li>The <code>src</code> argument refers to an object that is not an
     *     array.
     * <li>The <code>dest</code> argument refers to an object that is not an
     *     array.
     * <li>The <code>src</code> argument and <code>dest</code> argument refer
     *     to arrays whose component types are different primitive types.
     * <li>The <code>src</code> argument refers to an array with a primitive
     *    component type and the <code>dest</code> argument refers to an array
     *     with a reference component type.
     * <li>The <code>src</code> argument refers to an array with a reference
     *    component type and the <code>dest</code> argument refers to an array
     *     with a primitive component type.
     * </ul>
     * <p>
     * Otherwise, if any of the following is true, an
     * <code>IndexOutOfBoundsException</code> is
     * thrown and the destination is not modified:
     * <ul>
     * <li>The <code>srcPos</code> argument is negative.
     * <li>The <code>destPos</code> argument is negative.
     * <li>The <code>length</code> argument is negative.
     * <li><code>srcPos+length</code> is greater than
     *     <code>src.length</code>, the length of the source array.
     * <li><code>destPos+length</code> is greater than
     *     <code>dest.length</code>, the length of the destination array.
     * </ul>
     * <p>
     * Otherwise, if any actual component of the source array from
     * position <code>srcPos</code> through
     * <code>srcPos+length-1</code> cannot be converted to the component
     * type of the destination array by assignment conversion, an
     * <code>ArrayStoreException</code> is thrown. In this case, let
     * <b><i>k</i></b> be the smallest nonnegative integer less than
     * length such that <code>src[srcPos+</code><i>k</i><code>]</code>
     * cannot be converted to the component type of the destination
     * array; when the exception is thrown, source array components from
     * positions <code>srcPos</code> through
     * <code>srcPos+</code><i>k</i><code>-1</code>
     * will already have been copied to destination array positions
     * <code>destPos</code> through
     * <code>destPos+</code><i>k</I><code>-1</code> and no other
     * positions of the destination array will have been modified.
     * (Because of the restrictions already itemized, this
     * paragraph effectively applies only to the situation where both
     * arrays have component types that are reference types.)
     *
     * @param      src      the source array.
     * @param      srcPos   starting position in the source array.
     * @param      dest     the destination array.
     * @param      destPos  starting position in the destination data.
     * @param      length   the number of array elements to be copied.
     * @exception  IndexOutOfBoundsException  if copying would cause
     *               access of data outside array bounds.
     * @exception  ArrayStoreException  if an element in the <code>src</code>
     *               array could not be stored into the <code>dest</code> array
     *               because of a type mismatch.
     * @exception  NullPointerException if either <code>src</code> or
     *               <code>dest</code> is <code>null</code>.
     */
    public static native void arraycopy(Object src,  int  srcPos,
                                        Object dest, int destPos,
                                        int length);

这是System.class中对arraycopy方法的定义,可以看出来这个方法是native 方法,所以在这里看不到具体的实现,arraycopy接收5个参数,src是源数组(被复制的数组),srcPos是源数组复制的起始位置,dest是目标数组,destPos是目标数组存放的起始位置,length是复制的长度。因为是静态方法,所以直接通过System.arraycopy()就可以调用了。。。有兴趣的小伙伴可以去试试看。。

[java]数组复制小结 for 、clone、 System.arraycopy

今天看了下ArrayList的源码,其中许多方法要用到数组的复制,而且全部使用的是System.arraycopy方法 public static native void arraycopy(O...

java 数组复制:System.arrayCopy 深入解析

java 数组复制:System.arrayCopy 深入解析  (2013-06-04 17:02:17) 转载▼ 标签:  java   system ...

java 数组复制:System.arrayCopy 深入解析

先看ArrayList源码中数组复制的代码:    其实ArrayList 就是一个数组的形式存放数据的. 没有高深的地方.他的性能在于他的索引能力, 正因为他是数组形式,所以索引元素的时候他表...

使用System.arraycopy()实现数组之间的复制

 System提供了一个静态方法arraycopy(),我们可以使用它来实现数组之间的复制。 其函数原型是: public static void arraycopy(Object src, ...

使用System.arraycopy()实现数组之间的复制 、 StringUtils.join的使用

数组的复制,及字符串转换

使用System.arraycopy()实现数组之间的复制

package cn.edu.henu.test.mytest; import java.util.Arrays; /** * * 从指定源数组中复制一...

Java之System.arraycopy实现数组拷贝-类似C语言的memcpy

如果是复制一个一位数组,那么改变复制后的数组并不影响原数组。但是如果复制一个二维数组,那么改变其中任何一个数组,那么另一个的值也发生了变化。开始不是很明白,后来上网查了查资料,理解了其中奥妙。  ...
  • lqhed
  • lqhed
  • 2016年06月27日 20:01
  • 2324

java数组的四种拷贝方法的性能分析:for、clone、System.arraycopy、Arrays.copyof

今天作者就Java数组的拷贝方法进行性能比较,多有不足,请大家指正!!! 1.for方法,用循环依次实现数组元素的拷贝。 2.clone方法,是Object类的方法,用来实现对象的复制操作。 3.Sy...

java 数组操作 System.arraycopy(),ArrayUtils

System.arraycopy 整块内存拷贝,效率高; 参考:Java 数组复制System.arraycopy性能分析 为了避免内存不稳定干扰和运行的偶然性结果,我在一开始的时候把所有空间申明...
  • kk_2015
  • kk_2015
  • 2017年06月14日 12:28
  • 87
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Java使用System.arraycopy复制数组
举报原因:
原因补充:

(最多只允许输入30个字)