Array提供了copy方法,Copy方法有四个重载
使用格式:
Array.Copy(数组1,数组2,长度);
Array.Copy(数组1,数组2,Int64);
Array.Copy(数组1,指定索引,数组2,指定索引,长度);
Array.Copy(数组1,指定索引,数组2,指定索引,Int64);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;//引用命名空间
using System.Threading.Tasks;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
int[] intArray1 = { 1, 2, 3, 4, 5, 6 };
int[] intArray2 = { 7, 8, 9, 10, 11, 12 };
Console.WriteLine("原Array1数组为:");
foreach (int outnum in intArray1) Console.Write(outnum + "\t");
Console.WriteLine();
Console.WriteLine("原Array2数组为:");
foreach (int outnum in intArray2) Console.Write(outnum + "\t");
Console.WriteLine();
int[] resultNum = new int[20];
Array.Copy(intArray1, resultNum, 6);
//表示将intArray1中数组元素从索引值0开始,取6个长度放入到resultNum中,在resultNum中从索引值0开始存放。
//Console.WriteLine("使用第一种重载");
//foreach (int outnum in resultNum) Console.Write(outnum + "\t");
Console.WriteLine("合并后的结果");
Array.Copy(intArray1, resultNum, 6);
Array.Copy(intArray2, 0, resultNum, 6, 6);
//从intArray2索引值从0开始取6个长度放入到resultNum中,并从索引值6开始进行存放。
foreach (int outnum in resultNum) Console.Write(outnum + "\t");
int[] resultNum2 = new int[7];
Array.Copy(resultNum, 0, resultNum2, 0, 7);
Console.WriteLine("拆分后的结果");
foreach (int outnum in resultNum2) Console.Write(outnum+"\t") ;
Console.ReadKey();
}
}
}