C#中Array类创建动态类型、长度的数组

来源:互联网 发布:js中const 编辑:程序博客网 时间:2024/05/03 05:07
Array类

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

命名控件: System

程序集:mscorlib

语法:public abstract class Array:ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuraEquatable

我们可以看出,Array是一个抽象类。

在博客http://blog.csdn.net/wangshubo1989/article/details/46985269中所诉的用方括号声明的数组,其实是使用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

*/


3、复制数组

复制数组会使数组实现ICloneable接口,这个接口定义的Clone()方法会创建数组的浅表副本。

但是如果数组的元素包含引用类型,则不复制元素,只复制引用。

除了Clone()之外,还有Array.Copy方法创建浅表副本,二者的区别详见博客

http://blog.csdn.net/wangshubo1989/article/details/47002667


4、排序

Array类使用QuickSort(快速排序)算法对数组中元素进行排序。Sort()方法需要数组中的元素实现IComparable接口。

(string、int都实现了IComparable接口)

同样,Sort方法有许多重载函数,下面简单介绍几个。

//使用Array中每个元素的IComparable实现,对整个一维Array中的元素进行排序。

public static void Sort(Array array)

例如:

string [] names = {"wang", "li", "liu", "zhao"};

Array.Sort(names);

如果对数组使用自定义类,就必须实现IComparable接口

例如:

Person类

public class Person : IComparable {

public Person() { }

public Person(string name, string sex) {

this.Name = name;

this.Sex = sex;

}

public string Name;

public string Sex;

public override string ToString() {

return this.Name + " " + this.Sex;

}

#region IComparable 成员

public int CompareTo(object obj) {

Person p = obj as Person;

if (p == null) {

throw new NotImplementedException();

}

return this.Name.CompareTo(p.Name);

}

#endregion

} 这里就可以对Person对象数组排序了:

Person[] persons = {

new Person("Lili", "Female"),

new Person("Heicer", "Male"),

new Person("Lucy", "Female") };

Array.Sort(persons);

foreach (Person p in persons){

Console.WriteLine(p);

}

有关更详细的Sort()的使用方法会在后面的Blog中更新。
0 0