ArrayList的源码中数组的拷贝用到该方法:

public static void arraycopy(Object src, --源数组
int srcPos, --源数组要复制的起始位置
Object dest, --目的数组;
int destPos, --目的数组放置的起始位置;
int length) --length:复制的长度(源数组)。

对于基本类型来说:

int[] is = {1,2,3,4,5};
int[] is2 = {4,0,9};
System.arraycopy(is, 2, is2, 1, 2);
is[2] = 99;
System.out.println("is=" + Arrays.toString(is));
System.out.println("is2=" + Arrays.toString(is2));

运行结果:

is=[1, 2, 99, 4, 5]
is2=[4, 3, 4]

对于引用类型来Person:

class Person {
String name;
Person(String n) {
this.name = n;
}
public String toString() {
return "Person [name=" + name + "]";
}
}

引用类型测试:

        Person[] p1 = {new Person("A"),new Person("B"),new Person("C")};
Person[] p2 = new Person[4];
p2[0] = new Person("kaixin");
System.arraycopy(p1, 0, p2, 1, p1.length);
System.out.println("改变前p1=" + Arrays.toString(p1));
System.out.println("改变前p2=" + Arrays.toString(p2));
p1[1].name = "CCCCC";
System.out.println("改变后p1=" + Arrays.toString(p1));
System.out.println("改变后p2=" + Arrays.toString(p2));

引用类型测试结果:

改变前p1=[Person [name=A], Person [name=B], Person [name=C]]
改变前p2=[Person [name=kaixin], Person [name=A], Person [name=B], Person [name=C]]
改变后p1=[Person [name=A], Person [name=CCCCC], Person [name=C]]
改变后p2=[Person [name=kaixin], Person [name=A], Person [name=CCCCC], Person [name=C]]

System.arraycopy的测试的更多相关文章

  1. java的system.arraycopy()方法

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

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

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

  3. Java性能漫谈-数组复制之System.arraycopy

    当我还年幼的时候,我很任性,复制数组也是,写一个for循环,来回倒腾,后来长大了,就发现了System.arraycopy的好处. 为了测试俩者的区别我写了一个简单赋值int[100000]的程序来对 ...

  4. 【Java基础】System.arraycopy()的使用详解

    由于在Java中System.arraycopy()方法在一维数组和二维数组中的表现不同,所以做了一个测试 public static void main(String[] args) { int[] ...

  5. System.arraycopy 怎么使用的?

    前言:看 ArrayList 的源码,发现 remove 方法主要依赖了 System.arraycopy() 方法实现的.所以需要了解一下这个方法如何使用.转载请注明出处:https://www.c ...

  6. Java中 System.arraycopy() 和 Arrays.copyOf()方法

    System.arraycopy() 和 Arrays.copyOf()方法 阅读源码的话,我们就会发现 ArrayList 中大量调用了这两个方法.比如:我们上面讲的扩容操作以及add(int in ...

  7. 002-jdk-数据结构-工具类Collections、Arrays、System.arraycopy

    常用备注 一.LIst to Array List<String> list = new ArrayList<String>(); Object[] array=list.to ...

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

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

  9. 求System.arraycopy的用法

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

随机推荐

  1. ExclusiveTouch

    Setting this property to true causes the receiver to block the delivery of touch events to other vie ...

  2. 【算法】C++用链表实现一个箱子排序附源代码详解

    01 箱子排序 1.1 什么是分配排序? 分配排序的基本思想:排序过程无须比较关键字,而是通过"分配"和"收集"过程来实现排序.它们的时间复杂度可达到线性阶:O ...

  3. esp32编程第一例 hollow word

    #include<stdio.h>#include"freertos/FreeRtos.h"#include"freertos/task.h"#in ...

  4. [转] ScalaTest测试框架

    [From] https://blog.csdn.net/hany3000/article/details/51033610 ScalaTest测试框架 2016年04月01日 02:49:35 阅读 ...

  5. 认识CSS中高级技巧之元素的显示与隐藏

    前端之HTML,CSS(八) CSS高级技巧 元素的显示与隐藏 CSS中有三个属性可以设置元素的显示于隐藏,分别是:display.visibility和overflow. display 隐藏元素: ...

  6. UGUI Slider的onValueChanged事件

    在本文,你将学到如何将UGUI Slider的onValueChanged事件进行统一管理. using System; using UnityEngine; using UnityEngine.UI ...

  7. vue使用nprogress页面加载进度条

    vue使用nprogress页面加载进度条 NProgress是页面跳转是出现在浏览器顶部的进度条 官网:http://ricostacruz.com/nprogress/ github:https: ...

  8. vscode安装golang插件失败问题

    vscode安装golang插件失败问题 dlv go-outline go-symbols gocode-gomod gocode 代码补全 godef 代码跳转 golint gopkgs gor ...

  9. 仔细说明CLOSE_WAIT状态的原因

    CLOSE_WAIT状态!!! 服务器收到客户端FIN报文后  服务器端口回复了ack,没有回复FIN报文. 1 说明客户端后面不会再向服务器发送用户层的数据了, 2 客户端 没有回复FIN 报文嘛 ...

  10. XNA项目基础

    XNA项目基础 using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Frame ...