Java 数组拷贝方法 System.arraycopy
System类提供的数组拷贝方法:
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
数组拷贝方法,在读ArrayList源码的时候,频繁遇到,刚开始,囫囵吞枣的一带而过,知道个大概意思就算了,
不过,读到下面这里的时候,就有点蒙圈了,这种时段,当然沉下心来,慢慢看看。
public E remove(int index) {
rangeCheck(index);
modCount++;
E oldValue = elementData(index);
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // clear to let GC do its work
return oldValue;
}
从方法上来看,就是移除list中指定位置的元素,后面的元素依次向前移位1个位置
从底层数组上来看,就是将数组指定位置之后的元素,依次往前移位1个位置(怎么是一样的)
然后就到了正题:
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
src : 原数组
srcPos : 原数组指定位置(指定位置开始)
dest : 目标数组
destPos : 目标数组指定位置(指定位置开始)
length : 移动的元素数量
一句话就是,把src数组从srcPos位置开始,移动 length 个元素 覆盖到 dest数组的destPos 位置开始的length个元素
当然,注意数组越界异常 : java.lang.ArrayIndexOutOfBoundsException
srcPos 、destPos 越界,算越界
length 小于0 , 大于 src,dest 数组 的长度,算越界
srcPos + length,destPos + length 也算越界
打完,收工
Java 数组拷贝方法 System.arraycopy的更多相关文章
- jdk提供的数组扩容方法:System.arraycopy
package chapter7; /* * jdk提供的扩容方法 * System.arraycopy */public class TestArrayjdk { public static voi ...
- Java数组拷贝的五种方法
在Java中有多种方法可以拷贝一个数组,到另外一个数组. 1.循环拷贝 在循环拷贝方法中,只需要利用i,移动指针即可复制所有数组到arrayB中. for(int i=0;i<arrayA.le ...
- Java 数组的三种创建方法,数组拷贝方法
public static void main(String[] args) {//创建数组的第一种方法int[] arr=new int[6];int intValue=arr[5];//Syste ...
- Java性能漫谈-数组复制之System.arraycopy
当我还年幼的时候,我很任性,复制数组也是,写一个for循环,来回倒腾,后来长大了,就发现了System.arraycopy的好处. 为了测试俩者的区别我写了一个简单赋值int[100000]的程序来对 ...
- Java数组合并方法学习。
参考博客: https://blog.csdn.net/liu_005/article/details/72760392 https://blog.csdn.net/jaycee110905/arti ...
- 数组的复制 --System.arraycopy()
import java.util.Arrays; public class HellowWorld { public static void main(String[] argv ) { int[] ...
- Java—数组和方法
数组 声明数组 数组类型[] 数组名;or 数据类型 数组名[]; 如:int[] scores; 分配空间 数组名 = new 数据类型[数组长度];如:scores = new int[5]; 以 ...
- JAVA 数组作为方法返回值—返回地址
package Code411;/*一个方法可以有0,1,多个 参数,但只能有0和1个返回值希望一个方法产生多个结果数据进行返回 数组作为方法的参数,传递进去的是数组的地址值. */public cl ...
- JAVA 数组作为方法参数—传递地址
package Code411;//数组作为方法参数—传递地址public class DodeArrayParam { public static void main(String[] args) ...
随机推荐
- LibreOJ 6285. 数列分块入门 9
题目链接:https://loj.ac/problem/6285 其实一看到是离线,我就想用莫队算法来做,对所有询问进行分块,但是左右边界移动的时候,不会同时更新数字最多的数,只是后面线性的扫了一遍, ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
- centos 7下部署grpc
gRPC 是一个高性能.开源和通用的 RPC 框架,面向移动和 HTTP/2 设计.目前提供 C.Java 和 Go 语言版本,分别是:grpc, grpc-java, grpc-go. 其中 C 版 ...
- 再遇ibatisNet
11年在Mr刘的带领下第一次接触ibatisnet ,当时Mr刘很详细的很讲了xml里的写法还有配置文件之类的,但是随着时间越来越久远.很多东西都开始淡忘了. 如今,再次和它相遇,依然觉得很亲切,虽然 ...
- Java基本语法之动手动脑
1.枚举类型 运行EnumTest.java 运行结果:false,false,true,SMALL,MEDIUM,LARGE 结论:枚举类型是引用类型,枚举不属于原始数据类型,它的每个具体值都引用一 ...
- IIS下载,WebClient().DownloadFile下载
new System.Net.WebClient().DownloadFile(serverPath, localPath); 有时候使用的时候,文件下载不下来.需要设置一下服务器上IIS的权限
- CALL transaction 的用法-传内表
使用memory (这个方法和第二种方式的区别是可以传输复选框的值) data: wfbomcom type rc29n. move-corresponding bom_key to wfbomc ...
- C#中DllImport用法
http://blog.csdn.net/u011981242/article/details/52622923 http://www.jb51.net/article/46384.htm 读取身份证 ...
- 转载:MongoDB之旅(超赞,适合初学者)
MongoDB是目前工作中经常使用到的NoSQL数据库. 本博客只记录相关理论知识和技巧,涉及到实践的部分都会单开Blog来记录实践过程. ------------------------------ ...
- 安装scrapy 出现error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools 错误
安装scrapy 出现以下 错误: error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C ...