System.arraycopy 怎么使用的?
前言:看 ArrayList 的源码,发现 remove 方法主要依赖了 System.arraycopy() 方法实现的。所以需要了解一下这个方法如何使用。转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9951819.html
源码
带注释的源码:
/**
* 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.
* <
不带注释的源码:
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
参数解释:
src: 源数组;
srcPos: 源数组要复制的起始位置;
dest: 目的数组;
destPos: 目的数组放置的起始位置;
length: 复制的长度。
从源码注释中就可以看出
1、拷贝过程:将 src 数组的下标为 srcPos 到 srcPos + length - 1 拷贝到 dest 数组下标为 destPos 到 destPos + length- 1 的位置。
2、这个也支持 src 和 dest 为同一个数组,这时会将原数组拷贝到临时数组里,然后再将临时数组的数据拷贝到目标数组。
3、如果 src 或者 dest 为 null ,则会抛空指针异常。
4、如果 src 或者 dest 不是数组,则会抛出 ArrayStoreException。
5、src,dest 不是同类型或者可以进行转换的数组,则会抛出 ArrayStoreException。
6、如果 srcPos + length > length或者 destPos + lenght > length,则会抛出 IndexOutOfBoundsException。
测试:
转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9951819.html
System.arraycopy 怎么使用的?的更多相关文章
- System.arraycopy()和Arrays.copyOf()的区别
先看看System.arraycopy()的声明: public static native void arraycopy(Object src,int srcPos, Object dest, in ...
- 求System.arraycopy的用法
public class Shuzufuzhi { public static void main(String args[]) { int myArray[]={1,2,3,4,5,6}; in ...
- System.arrayCopy()和普通数组复制之间的效率差别
都是System.arrayCopy() 效率高,到底有多高呢,拉出来遛遛就知道了: package JCF.ArrayList; import java.util.Date; public clas ...
- java System.arraycopy 数组复制和合并
public class Test { public static void main(String[] args) { Integer[] a = {1,2,3}; Integer[] b = {4 ...
- Exception raised during rendering: java.lang.System.arraycopy([CI[CII)V
最近下载一个新版本的adt-bundle,Android API是20. 把Plain Text控件往布局上面拖时,发现拖不上去,出现了下面的错误: Exception raised during r ...
- Java数组的复制Arrays.copyOf()、System.arraycopy()、nums.clone()
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length); a ...
- java的system.arraycopy()方法
java.lang.System的静态方法arraycopy()可以实现数组的复制,讲课的老师说这个方法效率比较高,如果数组有成千上万个元素,那么用这个方法,比用for语句循环快不少.于是我试了试,发 ...
- Java-Java中System.arraycopy() 和 Arrays.copyOf()两者之间的区别
如果我们想拷贝一个数组,我们可能会使用System.arraycopy()或者Arrays.copyof()两种方式.在这里,我们将使用一个比较简单的示例来阐述两者之间的区别. 1.示例代码: Sys ...
- [Java基础] System.arraycopy使用
转载自:http://blog.csdn.net/java2000_net/article/details/4059465 System提供了一个native 静态方法arraycopy(),我们可以 ...
- 使用System.arraycopy()实现数组之间的复制
System提供了一个静态方法arraycopy(),我们可以使用它来实现数组之间的复制. 其函数原型是: public static void arraycopy(Object src, int s ...
随机推荐
- Number of subarrays having sum exactly equal to k(explanation for 437. Path Sum III of leetcode)
https://www.geeksforgeeks.org/number-subarrays-sum-exactly-equal-k/ 讲解的详细 看这道题是为了解决https://leetcode. ...
- js实现抽奖
抽奖.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- socketserver实现FTP
功能: 1.用户加密认证 2.允许同时多用户登录 3.每个用户有自己的家目录 ,且只能访问自己的家目录 4.对用户进行磁盘配额,每个用户的可用空间不同 5.允许用户在ftp server上随意切换目录 ...
- Linux下的redis的持久化,主从同步及哨兵
redis持久化 Redis是一种内存型数据库,一旦服务器进程退出,数据库的数据就会丢失, 为了解决这个问题,Redis提供了两种持久化的方案,将内存中的数据保存到磁盘中,避免数据的丢失. RDB持久 ...
- Go语言运算符
目录 算术运算符 注意事项 赋值运算符 逻辑运算符 短路与和短路或 关系运算符 位运算符 其他运算符 运算符优先级 运算符用于在程序运行时执行数学或逻辑运算. Go 语言内置的运算符有:算术运算符.赋 ...
- Linux学习笔记之三————Linux命令概述
一.引言 很多人可能在电视或电影中看到过类似的场景,黑客面对一个黑色的屏幕,上面飘着密密麻麻的字符,梆梆一顿敲,就完成了窃取资料的任务. Linux 刚出世时没有什么图形界面,所有的操作全靠命令完成, ...
- Dapper内部分享ppt
http://www.knowsky.com/887446.htmlhttp://www.tritac.com/bp-24-dapper-net-by-examplehttp://www.cnblog ...
- android_安装包_NoClassDefFoundError
说说这个问题出现的地方吧: 能够成功的打包安装包,但是在安装包安装后,准备运行时出现了这个问题. 查看了这篇文章,讲得有理有据,并没有解决我的问题. 通过谷歌查找到这个stackoverflow,解决 ...
- Visual Studio 2012 智能提示功能消失解决办法
安装为Visual Studio 2012且更新到了Update3,但是发现智能提示功能用不了,查了一下,网上各种资料,重装VS,连重装系统的都有.不过有很多朋友都是使用命令行重置VS的方法解决了这个 ...
- 图像处理之Retinex增强算法(SSR、MSR、MSRCR)
视网膜-大脑皮层(Retinex)理论认为世界是无色的,人眼看到的世界是光与物质相互作用的结果,也就是说,映射到人眼中的图像和光的长波(R).中波(G).短波(B)以及物体的反射性质有关 其中I是人眼 ...