拼接数组的几种方法

有时我们可以需要拼接数组,下面介绍拼接数组的几种方法:
1,使用数组的CopyTo方法:
int[] array1 = new int[2];
int[] array2 = new int[5];
int[] result = new int[array1.Length + array2.Length];

array1.CopyTo(result, 0);
array2.CopyTo(result, array1.Length);

2,和上面的方法类似,可以使用Array类的静态Copy方法:
int[] array1 = new int[2];
int[] array2 = new int[5];   
 int[] result=new int[array1.Length+array2.Length];
Array.Copy(array1,0,result,0,array1.Length);
Array.Copy(array2,0,result,array1.Length,array2.Length);

3,也可以利用一个list对象来拼接,然后转换成数组
int[] array1 = new int[2];
int[] array2 = new int[5];
list<int> tempList =new List<int>( );
tempList.Addrange(array1);
tempList.Addrange(array2);

result=tempList.ToArray();


转载于:https://www.cnblogs.com/xuefeng1982/archive/2009/07/24/1530380.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值