System.arraycopy

public static native void arraycopy(Object src,  int  srcPos, Object dest, int destPos, int length);

参数说明:

src:源数组;

srcPos:源数组要复制的起始位置;

dest:目的数组;

destPos:目的数组放置的起始位置;

length:复制的长度

原理:

实现过程是这样的,先生成一个长度为length的临时数组,将src数组中srcPos 到srcPos+length-1之间的数据拷贝到临时数组中,再执行System.arraycopy(临时组,srcPos,dest,destPos,length).

注意:

1、src 和 dest都必须是同类型或者可以进行转换类型的数组.

2、srcPos、srcPos+length-1都必须小于src的size,destPos、destPos+length-1也必须小于dest的size。

3、临时数组必须能一一对应到dest数组中。

例:

 int[] fun ={0,1,2,3,4,5,6};
 int[] fun1 = {7,8,9,10};
 /**
  * 两个不同数组间的拷贝
  * 1、拷贝fun中索引3到5(3+3-1)数据到临时数组中,生成临时数组int[] tmp = {3,4,5}。
  * 2、替换fun1中索引从1到3(1+3-1)的元素。
  */
 System.arraycopy(fun, 3, fun1, 1, 3);//结果为fun不变,fun1变为{7,3,4,5}

 /**
  * 同一个数组自己到自己的拷贝
  * 1、拷贝fun中索引3到6(3+4-1)数据到临时数组中,生成临时数组int[] tmp = {3,4,5,6}
  * 2、替换fun中索引2到5(2+4-1)的元素。
  */
 System.arraycopy(fun, 3, fun, 2, 4);//结果fun为{0,1,3,4,5,6,6}
时间: 2024-08-08 02:18:27

System.arraycopy的相关文章

android 布局页面文件出错故障排除Exception raised during rendering: java.lang.System.arraycopy([CI[CII)V

今天在看布局文件的时候出现 android 布局页面文件出错故障排除Exception raised during rendering: java.lang.System.arraycopy([CI[CII)V 提醒,google后在网上说是因为sdk版本的问题. 解决方法: 修改选择不同的API就好了,降低版本即可

How to solve Exception raised during rendering: java.lang.System.arraycopy([CI[CII)V in Android

android开发打开别人的项目的时候,手机面板上的控件有时候不能显示,还显示错误信息: Exception raised during rendering: java.lang.System.arraycopy([CI[CII)V Exception details are logged in Window > Show View > Error Log 原因是目前采用的API版本与原来的API版本不匹配,把API版本改一下即可.

System.arraycopy()和Arrays.copyOf()的区别

先看看System.arraycopy()的声明: public static native void arraycopy(Object src,int srcPos, Object dest, int destPos,int length); src - 源数组. srcPos - 源数组中的起始位置. dest - 目标数组. destPos - 目标数据中的起始位置. length - 要复制的数组元素的数量. 该方法用了native关键字,说明调用的是其他语言写的底层函数. 再看Arra

Android的布局文件遇到Exception raised during rendering: java.lang.System.arraycopy([CI[CII)V问题

打开xml的布局文件,发现布局无法显示预览,而且报错:Exception raised during rendering: java.lang.System.arraycopy([CI[CII)V.原来是因为用了最新的API 20.这个是Android用于开发可穿戴设备的,不支持EditText.将API改为20之前的就行了.

System.arraycopy和Arrays.copyOf的比较

public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length); 用了native关键字,调用的为C++编写的底层函数,可见其为JDK中的底层函数. public static int[] copyOf(int[] original, int newLength) { int[] copy = new int[newLength]; System.arraycopy

java 之System.arraycopy() vs arrays.copyOf()

在java中,数组的复制可以有System.arraycopy与arrays.copyOf()两种选择,下面就详细介绍一下这两种方法的差别: System.arraycopy int[] src = {1,2,3,4,5}; int[] des = new int[10]; System.arraycopy(arr, 0, copied, 1, 5); //5 is the length to copy System.out.println(Arrays.toString(des)); 输出结果

System.arraycopy 和Arrays.copyOf

1.Arrays.copyOf()的实现是用的是arrayCopy(); 2.System.arrayCopy()需要目标数组,对两个数组的内容进行可能不完全的合并操作. 3.Arrays.copyOf()在内部新建一个数组,调用System.arrayCopy()将original内容复制到copy中去,并  且长度为newLength.返回copy; 所以,使用System.arrayCopy()必须确定原数组不为null,且新数组的容量必须大于原数组 Arrays.copyOf()则已经新

Java数组的复制Arrays.copyOf()和System.arraycopy()函数

public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length); arraycopy是个本地方法,无返回值. public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) { T[] copy = ((Object)newType ==

System.arraycopy() or Arrays.copyOf()

1. Simple Code Examples System.arraycopy() int[] arr = {1,2,3,4,5}; int[] copied = new int[10]; System.arraycopy(arr, 0, copied, 1, 5);//5 is the length to copy System.out.println(Arrays.toString(copied)); Output: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [0, 1

Java System.arraycopy

System.arraycopy方法:如果是数组比较大,那么使用System.arraycopy会比较有优势,因为其使用的是内存复制,省去了大量的数组寻址访问等时间 public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length); 复制指定源数组src到目标数组dest.复制从src的srcPos索引开始,复制的个数是length,复制到dest的索引从destPo