System.arraycopy使用

        在查看Java代码时,经常会碰到这个函数System.arraycopy,为了以后的方便,在这里总结下。在Java中有System.arrayCopy使用

arraycopy的说明和使用

        arraycopy的定义如下:

	public static native void arraycopy(Object src,  
										int  srcPos,
										Object dest, 
										int destPos,
										int length);

        该函数是将src源数组中srcPos至srcPos+length-1的内容拷贝到dest目标数组中的destPos+length-1中去。从函数的定义来看,该函数为native函数,所以其实现不在Java层,这样获取能够使执行更有效率。函数参数说明如下:

        src:源数组

        srcPos:源数组要复制的起始位置

        dest:目的数组

        destpos:目的数组放置的起始位置

        length:复制的长度

        为了体验该函数的用法,废话不再说,直接上code喽!!

	private void intCopyDemo() {
		int[] mCopySrcInt = {1, 0, 0, 8, 6, 6, 8, 0, 0, 1};
		int[] mCopyDstInt = new int[mCopySrcInt.length]; 
		
		System.out.println("Destination array before arraycopy");
		for (int num : mCopyDstInt) {
			System.out.print("" + num + ", ");
		}
		System.out.println();
		
		System.arraycopy(mCopySrcInt, 0, mCopyDstInt, 0, mCopySrcInt.length);
		
		System.out.println("Destination array after arraycopy");
		for (int num : mCopyDstInt) {
			System.out.print("" + num + ", ");
		}
	}
        运行结果如下:

        Destination array before arraycopy
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        Destination array after arraycopy
        1, 0, 0, 8, 6, 6, 8, 0, 0, 1, 

arraycopy使用注意事项

        虽然使用arraycopy很方便,但是还是有一些注意事项的。

        1、首先保证复制和保存的数组不为空,并且复制的内容和保存的内容不超出src和dest数组,否则会报错。这不废话吗?

        2、尽量保证src和dest的类型一致性,数组的数据最好为基本类型,否则复制后会导致源数组和目标数组共用内存,容易引起错误。

        下面就用一个二维数组的例子来说明下吧。

	private void twoDimensionalCopyDemo() {
		String[][] mCopySrcTwoDmn = {{"Asia", "China", "GuangDong", "GuangZhou"}, 
				{"Beijing", "ShangHai", "GuangZhou", "ShenZhen"}};
		String[][] mCopyDstTwoDmn = new String[mCopySrcTwoDmn.length][mCopySrcTwoDmn[0].length];
		
		System.out.println("Before Two-Dimensional Copy");
		for (int idx = 0; idx < mCopyDstTwoDmn.length; idx++) {
			for (String str : mCopyDstTwoDmn[idx]) {
				System.out.print("" + str + ", ");
			}
			System.out.println();
		}

		System.arraycopy(mCopySrcTwoDmn, 0, mCopyDstTwoDmn, 0, mCopyDstTwoDmn.length);
		
		System.out.println("\nAfter Two-Dimensional arraycopy");
		for (int idx = 0; idx < mCopyDstTwoDmn.length; idx++) {
			for (String str : mCopyDstTwoDmn[idx]) {
				System.out.print("" + str + ", ");
			}
			System.out.println();
		}
		
		/*Modify the destination array. */
		mCopyDstTwoDmn[1][0] = "Africa";
		mCopyDstTwoDmn[1][2] = "Europe";
		
		System.out.println("\nAfter Two-Dimensional modify");
		for (int idx = 0; idx < mCopySrcTwoDmn.length; idx++) {
			for (String str : mCopySrcTwoDmn[idx]) {
				System.out.print("" + str + ", ");
			}
			System.out.println();
		}
	}
        运行结果如下:

        Before Two-Dimensional Copy
        null, null, null, null, 
        null, null, null, null, 


        After Two-Dimensional arraycopy
        Asia, China, GuangDong, GuangZhou, 
        Beijing, ShangHai, GuangZhou, ShenZhen, 


        After Two-Dimensional modify
        Asia, China, GuangDong, GuangZhou, 
        Africa, ShangHai, Europe, ShenZhen, 

        很明显,当修改目标数组时,源数组也随着变化了。这到底为什么会这样呢?

        java其实没有二维数组的概念,平常实现的二维数组只是元素是一维数组的一维数组,而数组也是引用类型,继承自Object类。数组是new出来的。这些性质也就导致arraycopy()二维数组时出现的问题(由此可见,如果是对象数组也会出现类似的问题)。 
        如果是一维数组,那么元素都是基础类型(如int,double等),使用arraycopy()方法后,是把原数组的值传给了新数组,属于值传递。而如果是二维数组,数组的第一维装的是一个一维数组的引用,第二维里是元素数值。对二维数组应用arraycopy()方法后,第一维的引用被复制给新数组的第一维,也就是两个数组的第一维都指向相同的“那些数组”。而这时改变其中任何一个数组的元素的值,其实都修改了“那些数组”的元素的值,所以原数组和新数组的元素值都一样了。

      

        如果需要完成整个二维数组的复制,而又不想引起以上问题时,可以采用一个一个一维数组复制的方式进行。

		for (int idx = 0; idx < mCopySrcTwoDmn.length; idx++) {
			System.arraycopy(mCopySrcTwoDmn[idx], 0, mCopyDstTwoDmn[idx], 0, mCopyDstTwoDmn[0].length);
		}

      好了,arraycopy的使用就到此结束。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值