前言:看 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 怎么使用的?的更多相关文章

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

    先看看System.arraycopy()的声明: public static native void arraycopy(Object src,int srcPos, Object dest, in ...

  2. 求System.arraycopy的用法

    public class Shuzufuzhi { public static void main(String args[]) {  int myArray[]={1,2,3,4,5,6};  in ...

  3. System.arrayCopy()和普通数组复制之间的效率差别

    都是System.arrayCopy() 效率高,到底有多高呢,拉出来遛遛就知道了: package JCF.ArrayList; import java.util.Date; public clas ...

  4. java System.arraycopy 数组复制和合并

    public class Test { public static void main(String[] args) { Integer[] a = {1,2,3}; Integer[] b = {4 ...

  5. Exception raised during rendering: java.lang.System.arraycopy([CI[CII)V

    最近下载一个新版本的adt-bundle,Android API是20. 把Plain Text控件往布局上面拖时,发现拖不上去,出现了下面的错误: Exception raised during r ...

  6. Java数组的复制Arrays.copyOf()、System.arraycopy()、nums.clone()

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

  7. java的system.arraycopy()方法

    java.lang.System的静态方法arraycopy()可以实现数组的复制,讲课的老师说这个方法效率比较高,如果数组有成千上万个元素,那么用这个方法,比用for语句循环快不少.于是我试了试,发 ...

  8. Java-Java中System.arraycopy() 和 Arrays.copyOf()两者之间的区别

    如果我们想拷贝一个数组,我们可能会使用System.arraycopy()或者Arrays.copyof()两种方式.在这里,我们将使用一个比较简单的示例来阐述两者之间的区别. 1.示例代码: Sys ...

  9. [Java基础] System.arraycopy使用

    转载自:http://blog.csdn.net/java2000_net/article/details/4059465 System提供了一个native 静态方法arraycopy(),我们可以 ...

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

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

随机推荐

  1. Repository 简化实现多条件查询

    Repository 在做查询的时候,如果查询条件多的话,linq查询表达式会写的很复杂,比如: public IQueryable<Student> Get(int id, string ...

  2. 利用Burp Suite攻击Web应用

    i春秋作家:Passerby2 web应用测试综述: Web应用漏洞给企业信息系统造成了很大的风险.许多web应用程序漏洞是由于web应用程序缺乏对输入的过滤.简而言之Web应用程序利用来自用户的某种 ...

  3. JS应用实例2:轮播图

    在学习轮播图之前,要先会切换图片: 找三张图片,命名1.jpg,2.jpg,3.jpg 示例: <!DOCTYPE html> <html> <head> < ...

  4. [Mac]macOS Mojave :发现 Mac 的新功能。

    1.深色模式 换种颜色看 Mac “深色模式”为桌面和内建应用带来更生动的外观,可让您轻松专注于最重要的内容. 若要在浅色和深色外观之间切换,请打开“系统偏好设置”并点按“通用”. 2.叠放 整理桌面 ...

  5. sublime text 3中安装ctags支持函数跳转,安装convertToUtf8支持中文步骤[工具篇]

    sublime text x是个很不错的编辑器,但是各种插件都需要自己安装,有时也有点不方便,尤其是自己还不不知道怎么安装的时候.开发中经常用到的,函数跳转,就是一个比较难安装的东西,记录如下(系统为 ...

  6. linux上面是否有安装redis,redis启动

    1.进入/usr/local/src目录,下载redis # cd /usr/local/src# wget http://download.redis.io/releases/redis-4.0.6 ...

  7. LeetCode--No.007 Reverse Integer

    7. Reverse Integer Total Accepted: 153147 Total Submissions: 644103 Difficulty: Easy Reverse digits ...

  8. django-pure-pagination实现分页

    django-pure-paginations是一个第三方的分页插件 安装 django-pure-pagination pip install django-pure-pagination 在set ...

  9. Python基础教程(第3版) 笔记(三)

    1.9.1让脚本像普通程序一样在UNIX中运行脚本,只需将下面的代码作为脚本的第一行, 就可在UNIX中轻松运行脚本: #!/usr/bin/env python 要像普通程序一样运行脚本,还必须将其 ...

  10. 夜神模拟已开启,adb命令检测不了设备解决方法

    日常APP测试中,很难拥有多种机型和各种安卓版本的手机,此时可以借助模拟器. 命令返回结果只有 “List of devices attached”,即代表检测不了模拟器 最近在使用夜神模拟器的时候, ...