拼接数组的几种方法

有时我们可以需要拼接数组,下面介绍拼接数组的几种方法:
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();


posted on 2009-07-24 17:16  周雪峰  阅读(4415)  评论(0编辑  收藏  举报