当前位置: 首页 > 图文教程 > .Net技术 > ASP.NET > ASP.NET:如何复制数组中一系列元素的元素

ASP.NET
ASP.NET:小编教你使用Substitution控件在缓存页面插入内容
ASP.NET:网络上实现单点登录
ASP.NET:小编浅谈泛型的使用
ASP.NET:网络验证的一些技巧杂谈
ASP.NET:小编教你实现数字和英文字母组合的验证码图片
ASP.NET:页面尺寸自动适应 1024*768和800*600分辨率
ASP.NET:如何复制数组中一系列元素的元素
ASP.NET:小编谈用Remoting技术传送文件

ASP.NET:如何复制数组中一系列元素的元素


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-17   浏览: 668 ::
收藏到网摘: n/a

本实例主要介绍ASP.NET如何使用ArrayCopy方法来复制数组中一系列的元素。Copy方法从指定的源索引开始,复制Array中的一系列元素,将他们粘贴到另一个Array中(从指定的目标索引开始)。长度和索引指定为64位整数。其方法有多种重载形式,本实例所使用的重载形式如下:

Public static void Copy (Array sourceArray,long sourceIndex,Array destinationArray,long destinationIndex,long length )

参数说明

sourceArray:Array,包含要复制的数据。

sourceIndex: 一个64位整数,表示sourceArray中复制开始处索引。

destinationArray: Array,接受数据。

destinationIndex: 一个64位整数,表示destinationArray中存储开始处的索引。

length: 一个64位整数,表示要复制的元素数目。该整数必须介于零和Int32.MaxValue之间(包括零和Int32.MaxValue)。

主要代码如下:

  Private void button1_Click(object sender,EventArgs e)

  {

     Array myIntArray=Array.CreateInstance(typeof(System.Int32),5)

     For(int i=myIntArray.GetLowerBound(0);i<=myIntArray.GetUpperBound(0);i++)

        myObjArray.SetValue(i+1,i)//给数组赋值

     Array myObjArray=Array/CreateInstance(typeof(System.Object),5)//定义一个长度为5一维数组;

     For(int i=myObjArray.GetLowerBound(0); i<=myObjArray.GetUpperBound(0));i++)

        myObjArray.SetValue(i+26,i);//给数组赋值

     string strMyIntArra=”myIntArray数组复制前”;

     string strmyObjArray=”myObjArray数组复制前”;

     for(int i=0;i<myIntArray.Length;i++)//取出数组中的值

     {

       StrMyIntArra=+myIntArray.GetValue(i).ToString()+”,”;

}

For(int i=0;i<myObjArray.Length;i++)//取出数组中的值

{

  strmyObjArray+=myObjArray.GetValue(i).ToString()+”,”;

}

String strMyIntArrayAfter=”myIntArray数组复制后:”;

String strmyObjArrayAfter=”myObjArray数组复制后:”;

Array.Copy(myIntArray.myIntArray.GetLowerBound(0),myObjArray,myObjArray.GetLowerBound(0),1);

//复制数组中2个元素

Array.Copy(myIntArray.myIntArray.GetUpperBound(0),myObjArray,myIntArray.GetUpperBound(0),-1,2);

//复制数组中3个元素

For(int i=0;i<myIntArray.Length;i++)//取出数组中的值

{

  strMyIntArraAfter+=myIntArray.GetValue(i).ToString()+”,”;

}

For(int i=0;i<myObjArray.Length;i++)//取出数组中的值

{

    strmyObjArrayAfter+=myObjArray.GetValue(i).ToString()+”,”;

}

MessageBox.Show(strMyIntArra+”\n”+strmyObjArray+”\n”+strMyIntArraAfter+”\n”+strmyObjArrayAfter,”复制数组中的元素MessageBoxButtons.OK,

MessageBoxIcon.Information)

 }