2017/12/30Java基础学习——复制数组のSystem.arraycopy()方法讲解

System.arraycopy方法格式:

1 System.arraycopy(Object src, int srcPos, Object dest, Int destPos, Int length)
  • src:源数组,source的缩写[填写被复制的数组名]
  • srcPos: 从源数组复制数据的起始位置,source-position[位置用数组的下标来表示]
  • dest:目标数组,destination的缩写
  • destPos: 复制到目标数组的起始位置
  • length:复制的长度

1)复制一维数组

代码案例如下:  

 1 import java.util.Arrays;
 2 
 3 public class HelloWorld {
 4      public static void main(String[] args) {
 5          int a [] = new int[]{18,62,68,82,65,9};   
 6          int b[] = new int[3];//分配了长度是3的空间,但是没有赋值
 7           
 8          //1、通过数组赋值把a数组的前3位赋值到b数组
 9           
10          //方法一: for循环         
11          for (int i = 0; i < b.length; i++) {
12              b[i] = a[i];
13          }
14          
15          //方法二: System.arraycopy(src, srcPos, dest, destPos, length)
16          //src: 源数组
17          //srcPos: 从源数组复制数据的启始位置
18          //dest: 目标数组
19          //destPos: 复制到目标数组的启始位置
20          //length: 复制的长度       
21          System.arraycopy(a, 0, b, 0, 3);
22           
23          //把内容打印出来
24          for (int i = 0; i < b.length; i++) {
25              System.out.print(b[i] + " ");//18 62 68 
26          }
27          
28          System.out.println();
29          //2、测试自我复制:即将数组a复制到数组a
30          System.arraycopy(a, 0, a, 3, 2);
31          System.out.println(Arrays.toString(a));//[18, 62, 68, 18, 62, 9]
32      }
33  }                                

2)复制二维数组

如果复制一个二维数组,那么对二维数组应用arraycopy()方法后,改变其中任何一个数组,那么另一个的值也发生了变化
 1 public class HelloWorld {
 2     public static void main(String[] args) {        
 3 //        如果是复制一个一维数组,那么改变复制后的数组并不影响原数组。
 4 //        但是如果复制一个二维数组,那么改变其中任何一个数组,那么另一个的值也发生了变化。
 5 //        java其实没有二维数组的概念,平常实现的二维数组只是元素是一维数组的一维数组,而数组也是引用类型,继承自Object类...
 6 //        数组是new出来的。这些性质也就导致arraycopy()二维数组时出现的问题。
 7 //        如果是一维数组,那么元素都是基础类型(如int,double等),使用arraycopy()方法后,是把原数组的值传给了新数组,属于值传递。
 8 //        而如果是二维数组,数组的第一维装的是一个一维数组的引用,第二维里是元素数值。
 9 //        对二维数组应用arraycopy()方法后,第一维的引用被复制给新数组的第一维,也就是两个数组的第一维都指向相同的“那些数组”。
10 //        而这时改变其中任何一个数组的元素的值,其实都修改了“那些数组”的元素的值,所以原数组和新数组的元素值都一样了。
11  
12         //二维数组  
13         int[][] s3 = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}};  
14         System.out.println("s3.length = " + s3.length +"
s3[0].length = " + s3[0].length );
15         int[][] s4 = new int[s3.length][s3[0].length];  
16         System.out.println("This is s3");  
17         System.arraycopy(s3, 0, s4, 0, s3.length);  
18         for (int[] aS3 : s3) {  
19             for (int j = 0; j < s3[0].length; j++) {  
20                 System.out.print(aS3[j] + ",");  
21             }  System.out.println();
22         }  
23       
24         s4[1][3] = 111;  
25         System.out.println("
This is s4");  
26         for (int[] aS4 : s4) {  
27             for (int j = 0; j < s4[0].length; j++) {  
28                 System.out.print(aS4[j] + ",");  
29             }  
30         }  
31         System.out.println("
This is s3");  
32         for (int[] aS3 : s3) {  
33             for (int j = 0; j < s3[0].length; j++) {  
34                 System.out.print(aS3[j] + ",");  
35             }  
36         }
37     }
38 }

优势

  若数组比较大,使用System.arraycopy比较有优势,因为其使用的是内存复制,省去了大量的数组寻址访问等时间

原文地址:https://www.cnblogs.com/lijiehua/p/8150878.html