首页 > 开发 > .Net > 正文

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

2020-02-03 15:57:04
字体:
来源:转载
供稿:网友
第十四章 数组.

内容摘要:

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



一、 数组简介

三种类型:一维数组、多维数组、交错数组(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 拓宽(widen)clr基类型,如将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、上述代码中涉及到类型转换操作符的应用,请参考读书笔记第九章 方法

四、 数组传递与返回

注意事项:

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

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()方法,将原来的数组拷贝到新数组中


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表