数组拷贝学习心得

src:原始数组,srcPos:原始数组索引位置,dest:目标数组,destPos:目标数组的索引位置,length:拷贝个数

System.arraycopy(src, srcPos, dest, destPos, length);

package com.guor.test.javaSE.collection;
 
import java.util.Arrays;
 
import com.guor.test.User;
 
public class ArrayTest {
 
	public static void main(String[] args) {
		beanCopy();
	}
	
	//**************************************************************
	private static void copySelf() {
		int[] ids = { 1, 2, 3, 4, 5 };  
		System.out.println(Arrays.toString(ids));
		//System.arraycopy(src, srcPos, dest, destPos, length);
		// 把从索引0开始的2个数字复制到索引为3的位置上  
		System.arraycopy(ids, 0, ids, 3, 2);
		System.out.println(Arrays.toString(ids));//[1, 2, 3, 1, 2]
	}
	
	private static void copyToOther() {
		int[] ids = { 1, 2, 3, 4, 5 };  
		//将数据的索引1开始的3个数据复制到目标的索引为0的位置上  
		int[] other = new int[5];
		System.arraycopy(ids, 1, other, 0, 3);
		System.out.println(Arrays.toString(ids));//[1, 2, 3, 4, 5]深复制
		System.out.println(Arrays.toString(other));//[2, 3, 4, 0, 0]
	}
}

System.arraycopy 总结
基本数组属于深copy:改变原始数组或者copy数组,另一个数组对应的引用对象值不变
多维数组和数组对象是浅copy:(改变原始数组或者copy数组,另一个数组对应的引用对象值一起改变

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值