高级的数组的复制(test4.java)

  这里指的高级,并不是过么高大上,而是说我们可以调用系统函数,直接对数组进行复制,并且这个函数的强大并不止局限于,对数组的复制,而且可以对数组进行截取,在指定位置插入或删除某个元素。

  本篇只介绍数组的复制,其他的操作将在后续文章中进行阐述。

  

  将一个数组复制到另一个数组去,采用

  System.arrycopy()

  方法的参数说明:

  System.arrycopy(from,fromstart,to,tostart,count)

  

 1 //将A数组值复制到B数组中
 2 
 3 public class test4
 4 {
 5     public static void main (String [] args)
 6     {
 7         int [] a = {1,2,3,4,5};
 8 
 9         int [] arr2 = new int [arr1.length];
10 
11         System.arrycopy(arr1,0,arr2,0,arr1.length);
12 
13         arr2[2] = 10;
14 
15         for(int num : arr1)
16         {
17             //打印结果:1 2 3 4 5
18             System.out.print(num+"\t");
19         }
20 
21         System.out.println();
22 
23         for(int num : arr2)
24         {
25             //打印结果为:1 2 10 4 5
26             System.out.print(num+"\t");
27         }
28     }
29 }

  

猜你喜欢

转载自www.cnblogs.com/zglbt/p/8934471.html