当前位置:首页 > Windows程序 > 正文

C#中Array类的使用

2021-03-29 Windows程序

提供创建、操作、搜索和排序数组的方法,因而在公共语言运行时中用作所有数组的基类。

命名控件: System 程序集:mscorlib 语法:public abstract class Array:ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuraEquatable 我们可以看出,Array是一个抽象类。 在博客中所诉的用方括号声明的数组,其实是使用Array类的表示法。在后台使用 C#语法,会创建一个派生自抽象基类Array的新类。 这样就可以使用Array类为每个C#数组定义的方法和属性了。 1、使用Array创建数组 前面已经提到, Array是一个抽象类,所以不能使用构造函数来创建数组。但是可以使用静态方法CreateInstance()创建数组。 CreateInstance()的重载版本有很多:


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

public static Array CreateInstance(Type elementType, int length)


例如:

Array my1DArray = Array.CreateInstance(typeopf(Int32), 5); //创建使用从零开始的索引、具有指定Type和维长的多维Array。维的长度在一个32位整数数组中指定。 public static Array CreateInstance(Type elementType, params int[] lengths) 例如: int [] myLengthsArray = new int[4] {2,3,4,5}; Array my2DArray = Array.CreateInstance(typeopf(string), myLengthsArray);


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

public static Array CreateInstance(Type elementType, params long[] lengths) 例如: int [] myLengthsArray = new int[4] {2,3,4,5}; Array my3DArray = Array.CreateInstance(typeopf(string), myLengthsArray);


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

public static Array CreateInstance(Type elementType, int length1, int length2) 例如: Array my4DArray = Array.CreateInstance(typeopf(string), 2, 3);//创建一个2*3的数组


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

public static Array CreateInstance(Type elementType, int [] lengths, int [] lowerBounds) 其中, lengths表示要创建的Array的每个维度的大小;lowerBounds表示要创建的Array的每个维度的起始索引。 例如: int [] lengths = {2, 3}; int [] lowerBounds{1, 10}; Array my5DArray = Array.CreateInstance(typeopf(string), lengths , lowerBounds);//创建一个2*3的数组,第一维基于1, 第二维基于10


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

public static Array CreateInstance(Type elementType, int length1, int length2, int length3) 例如: Array my5DArray = Array.CreateInstance(typeopf(string), 2, 3, 4);


2、设置和获取数组的元素

使用SetValue()函数设置数组的元素,SetValue()有许多重载的版本 使用GetValue()函数来获取数组的元素,GetValue()有许多重载的版本 例如: // Creates and initializes a one-dimensional array. String[] myArr1 = new String[5]; // Sets the element at index 3. myArr1.SetValue( "three", 3 ); Console.WriteLine( "[3]: {0}", myArr1.GetValue( 3 ) ); // Creates and initializes a two-dimensional array. String[,] myArr2 = new String[5,5]; // Sets the element at index 1,3. myArr2.SetValue( "one-three", 1, 3 ); Console.WriteLine( "[1,3]: {0}", myArr2.GetValue( 1, 3 ) ); // Creates and initializes a three-dimensional array. String[,,] myArr3 = new String[5,5,5]; // Sets the element at index 1,2,3. myArr3.SetValue( "one-two-three", 1, 2, 3 ); Console.WriteLine( "[1,2,3]: {0}", myArr3.GetValue( 1, 2, 3 ) ); // Creates and initializes a seven-dimensional array. String[,,,,,,] myArr7 = new String[5,5,5,5,5,5,5]; // Sets the element at index 1,2,3,0,1,2,3. int[] myIndices = new int[7] { 1, 2, 3, 0, 1, 2, 3 }; myArr7.SetValue( "one-two-three-zero-one-two-three", myIndices ); Console.WriteLine( "[1,2,3,0,1,2,3]: {0}", myArr7.GetValue( myIndices ) ); } } /* 上诉代码的输出: [3]: three [1,3]: one-three [1,2,3]: one-two-three [1,2,3,0,1,2,3]: one-two-three-zero-one-two-three */


温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/69447.html