第十四章 数组[《.net框架程序设计》读书笔记]

来源:互联网 发布:python excel 修改 编辑:程序博客网 时间:2024/05/22 05:04
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

第十四章 数组.

内容摘要:

       本章讨论了数组的方方面面,对于这种常用类型进行深入研究。

 

一、             数组简介

       三种类型:一维数组、多维数组、交错数组(jagged aray)

l         一维数组

       Int32[] myIntegers;

       myIntegers = new Int32[100];

l         多维数组

Int32[,] myIntegers;

       myIntegers = new Int32[100,100];

l         交错数组:交错数组不受CLS支持

       Point[][] myPolygons = new Point[3][];

       myPolygons[0] = new Point[10];

       myPolygons[1] = new Point[20];

       myPolygons[2] = new Point[30];

二、             System.Array

       请参考.net framework sdk中相关描述

三、             数组转型

l         数组必须有同样的维数

l         数组中元素类型间存在隐式或显式转换

l         除使用Array.Copy()方法外,不允许将值类型数组转换为其他类型数组Array.Copy方法会根据需要进行强制类型转换或装箱操作)

Array.Copy()方法能够执行的类型转换如下:

l         将值类型转换为引用类型,将Int32转换为Object

l         将引用类型转换为值类型,将Object转换为Int32

l         拓宽(widenCLR基类型,如将Int32转换为Double

 

下面这个示例提供了一个反面教材(切勿效仿,后果自负!)了值类型的装换:

using System;

//自定义一个值类型,该类型可以与Int32进行互转换

struct MyAge

{

       private Int32 m_nAge;  

      

       public MyAge(Int32 nAge)

       {

              this.m_nAge = nAge;    

       }

      

       //转换操作符

       public static implicit operator MyAge(Int32 nAge)

       {

              return new MyAge(nAge);    

       }

      

       public static explicit operator Int32(MyAge age)

       {

              return age.ToInt32();    

       }

      

       public Int32 ToInt32()

       {

              return m_nAge;     

       }

      

       public override string ToString()

       {

              return "Age : " + m_nAge;

       }

}

 

public class ArrayTest

{

       public static void Main()

       {

              Int32[] arrInt = new Int32[10];

              for(int i = 0; i < arrInt.Length; i ++)

              {

                     arrInt[i] = i;   

              }    

             

              MyAge[] arrAge = new MyAge[arrInt.Length];

              Array.Copy(arrInt, arrAge, arrInt.Length);

             

              foreach(MyAge age in arrAge)

              {

                     Console.WriteLine(age.ToString());     

              }

       }    

}

 

/*运行结果

未处理的异常: System.ArrayTypeMismatchException: 不能将源数组类型分配给目标数组

类型。

   at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationA

rray, Int32 destinationIndex, Int32 length)

   at System.Array.Copy(Array sourceArray, Array destinationArray, Int32 length)

 

   at ArrayTest.Main()

 

*/

注:1上述代码是不能运行的。虽然为值类型定义了转换操作,但仍不满足上述转换条件,可见自定义的转换操作不被Array.Copy()认可。

       2、上述代码中涉及到类型转换操作符的应用,请参考读书笔记net/sayo/archive/2004/07/02/32324.aspx">第九 方法

四、             数组传递与返回

       注意事项:

l         为获得对数组元素的深拷贝,要求每个元素都实现ICloneable接口。需要注意的是应在适当时候使用深拷贝操作(详情请参见《c# primerp185)

l         返回数组引用的方法若不含数组元素应该返回一个包含0个元素的数组而不是null

l         不在方法中返回类型内部的数组引用,而是重新构造一个深拷贝的数组返回。

五、             创建下限非0数组

       使用Array.CreateInstance()方法:

       public static Array CreateInstance(Type elementType, int[] lengths, int[] lowerBounds);

       elementType   :      要创建的 Array Type

       lengths    :      一维数组,它包含要创建的 Array 的每个维度的大小

       lowerBounds   :      一维数组,它包含要创建的 Array 的每个维度的下限(起始索引)。

 

六、             快速数组访问

要点:

l         使用非安全的代码

l         使用指针访问数组

l         可进行非安全数组操作的元素为数值基元类型、Boolean、枚举类型或字段为上述类型的结构类型,或字段为上述结构类型的结构类型……(递归定义的结构类型)

如下面例程所示:

using System;

 

public class ArrSafe

{

       unsafe public static void Main()    //此处表明使用非安全代码

       {

              Int32[] arr = new Int32[]{1,2,3,4,5};

              fixed(Int32 *element = arr)    //使用指针访问

              {

                     for(Int32 i = 0; i < arr.Length; i ++)

                     {

                            Console.WriteLine(element[i]);     

                     }

              }

       }

}

七、             重新调整数组的长度

l         使用如下方法获取创建新的数组长度(请参见.net framework sdk中的详细描述)

创建使用从零开始的索引、具有指定 Type 和长度的一维 Array。

[C#] public static Array CreateInstance(Type, int);

创建使用从零开始的索引、具有指定 Type 和维长的多维 Array。维的长度在一个 32 位整数数组中指定。

[C#] public static Array CreateInstance(Type, params int[]);

创建使用从零开始的索引、具有指定 Type 和维长的多维 Array。维的长度在一个 64 位整数数组中指定。

[C#] public static Array CreateInstance(Type, params long[]);

创建使用从零开始的索引、具有指定 Type 和维长的二维 Array。

[C#] public static Array CreateInstance(Type, int, int);

创建具有指定下限、指定 Type 和维长的多维 Array。

[C#] public static Array CreateInstance(Type, int[], int[]);

创建使用从零开始的索引、具有指定 Type 和维长的三维 Array。

[C#] public static Array CreateInstance(Type, int, int, int);

 

l         然后使用Array.Copy()方法,将原来的数组拷贝到新数组

 

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击