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


相关文章:

  • 2022-12-23
  • 2021-08-16
  • 2022-02-07
  • 2022-01-21
  • 2021-11-09
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-07-22
  • 2022-12-23
  • 2021-08-11
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案