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

时间:2023-11-20 10:52:26

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

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

  

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

  System.arraycopy()

  方法的参数说明:

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

  

 //将A数组值复制到B数组中

 public class test4
{
public static void main (String [] args)
{
int [] arr1 = {1,2,3,4,5}; int [] arr2 = new int [arr1.length]; System.arraycopy(arr1,0,arr2,0,arr1.length); arr2[2] = 10; for(int num : arr1)
{
//打印结果:1 2 3 4 5
System.out.print(num+"\t");
} System.out.println(); for(int num : arr2)
{
//打印结果为:1 2 10 4 5
System.out.print(num+"\t");
}
}
}