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 ...
随机推荐
- PMP:4.项目整合管理
内容中包含 base64string 图片造成字符过多,拒绝显示
- WCF双工通信单工通信
1.单工模式 单向通信,指通信只有一个方向进行,即从客户端流向服务,服务不会发送响应,而客户端也不会期望会有响应.这种情况下,客户端发送消息,然后继续执行 运行后报错: 2.双工模式 双工模式的特点是 ...
- ubuntu16.04安装maven
一.下载maven apache maven官网地址:http://maven.apache.org/download.cgi 找到Link列下的“apache-maven-3.5.2-bin.tar ...
- 基于ε-NFA的正则表达式引擎
正则表达式几乎每个程序员都会用到,对于这么常见的一个语言,有没有想过怎么去实现一个呢?乍一想,也许觉得困难,实际上实现一个正则表达式的引擎并没有想像中的复杂,<编译原理>一书中有一章专门讲 ...
- 优化版小程序canvas,增加失败逻辑,及完善文字
wxml <view class="shareBox" style="backgound:{{isShow ? '#000' : '#fff'}}" wx ...
- Scala微服务架构 三
四 Controller层 之前我们已经把基层架构搭建好了,那么要如何使用呢? 首先看看我的Controller层代码 @Singleton class BMAuthController @Injec ...
- 学生成绩管理系统(C++指针、链表、文件及面向对象的运用)
学生成绩管理系统 功能页面显示: 实现源码: #include<iostream> #include<fstream> #include<cstring> # ...
- spring boot、cloud v2.1.0.RELEASE 使用及技术整理
2018年10月30日 springboot v2.1.0.RELEASE 发布: https://github.com/spring-projects/spring-boot/releases/ta ...
- Setting Up Swagger 2 with a Spring REST API
Last modified: August 30, 2016 REST, SPRING by baeldung If you're new here, join the next webinar: & ...
- 第一篇:服务的注册与发现Eureka(Finchley版本)
一.创建服务注册中心(Eureka) 1. 首先创建一个maven主工程 创建一个主Maven工程,在其pom文件引入依赖,spring Boot版本为2.0.3.RELEASE,Spring Clo ...