Array类
创建数组
Array intArray1 = Array.CreateInstance(typeof(int), 5);
for (int i = 0; i < 5; i++)
{
intArray1.SetValue(33, i);
}
for (int i = 0; i < 5; i++)
{
Console.WriteLine(intArray1.GetValue(i));
}
Console.ReadKey();
分析:array类是一个抽象类,所以不能使用构造函数来创建数组(不能使用new).但除了可以使用C#语法创建数组实例之外,还可以使用静态方法CreateInstance()创建数组.如果事先不知道元素的类型,该静态方法就非常游泳.因为类型可以作为Type对象传递给CreateInstance()方法.
还可以将使用Array类创建的数组强制转换成声明为int[]的数组:
int[] intArray2 = (int[])intArray1;
CreateInstance()方法有许多重载版本,可以创建多维数组和不基于0的数组:
using System;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
int[] lengths = { 2, 3 };
int[] lowerBounds = { 1, 10 };
Array racers = Array.CreateInstance(typeof(Person), lengths, lowerBounds);
racers.SetValue(new Person("a", "b"), 1, 10);
racers.SetValue(new Person("c", "d"), 1, 11);
racers.SetValue(new Person("e", "f"), 1, 12);
racers.SetValue(new Person("g", "h"), 2, 10);
racers.SetValue(new Person("i", "j"), 2, 11);
racers.SetValue(new Person("k", "l"), 2, 12);
Person[,] racers1 = (Person[,])racers;
Person first = racers1[1, 10];
}
}
public class Person
{
public Person()
{ }
public Person(string firstName, string lastName)
{
this.FirstName = firstName;
LastName = lastName;
}
public string FirstName { get; set; }
public string LastName { get; set; }
public override string ToString()
{
return String.Format("{0} {1}", FirstName, LastName);
}
}
}
使用SetValue()方法设置数组的元素,参数是:要设置的值,和当前索引号.
复制数组
因为数组是引用类型,所以江数组变量赋予另一个数组变量,就会得到两个引用同一数组的变量.而复制数组,会使数组实现ICloneable接口.这个接口定义的Clone()方法会创建数组的浅表副本.
如果数组的元素是值类型,则:
如果数组包含引用类型,则不复制元素,而知复制引用.
除了使用Clone()方法之外,还可以使用Array.Copy()方法创建浅表副本.但Clone()方法和Copy()方法有一个重要区别:Clone()方法会创建一个新数组,而Copy()方法必须传递阶数相同且有足够元素的已有数组.
如果需要包含引用类型的数组的深层副本,就必须迭代数组并创建对象.
排序
string[] names = {"zhao","qian","sun","li" };
Array.Sort(names);
foreach (var item in names)
{
Console.WriteLine(item);
}
Console.ReadKey();
排序后的结果:
li
qian
sun
zhao
由此看见,排序是按照字典序排列.
如果对数组使用自定义类,就必须事先IComparable接口.这个接口定义了一个方法CompareTo(),如果要比较的对象相等,该方法就返回0.如果该实例应拍在参数对象的前面,该方法就返回小于0的值.如果该实例应排在参数对象的后面,该方法就返回大于0的值.
案例:
public class Person:IComparable<Person>
{
public Person()
{ }
public Person(string firstName, string lastName)
{
this.FirstName = firstName;
LastName = lastName;
}
public string FirstName { get; set; }
public string LastName { get; set; }
public override string ToString()
{
return String.Format("{0} {1}", FirstName, LastName);
}
public int CompareTo(Person other)
{
if (other==null)
{
throw new ArgumentNullException("other");
}
int result = this.LastName.CompareTo(other.LastName);
if (result==0)
{
result = this.FirstName.CompareTo(other.FirstName);
}
return result;
}
}
编写测试代码:
static void Main(string[] args)
{
Person[] persons ={
new Person{FirstName="shan",LastName="yongxu"},
new Person{FirstName="sun",LastName="yanzhao"},
new Person{FirstName="zhu",LastName="haitao"},
new Person{FirstName="wang",LastName="jiwei"}
};
Array.Sort(persons);
foreach (var item in persons)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
这样是按照名排序,不是按照姓排序.
如果Person对象的排序方式与上述不同,或者不能修改在数组中用作元素的类,就可以实现IComparer接口或IComparer<T>接口.这两个接口定义了方法Compare().要比较的类必须实现这两个接口之一.ICompare接口的Compare()方法定义了两个要比较的参数的原因.其返回值与IComparable接口的CompareTo()方法类似.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public enum PersonCompareType
{
FirstName,
LastName
}
public class PersonComparer : IComparer<Person>
{
private PersonCompareType compareType;
public PersonComparer(PersonCompareType compareType)
{
this.compareType = compareType;
}
public int Compare(Person x, Person y)
{
if (x==null||y==null)
{
throw new ArgumentNullException("x==y");
}
switch (compareType)
{
case PersonCompareType.FirstName:
return x.FirstName.CompareTo(y.FirstName);
case PersonCompareType.LastName:
return x.LastName.CompareTo(y.LastName);
default:
throw new ArgumentNullException("type");
}
}
}
class Program
{
static void Main(string[] args)
{
Person[] persons ={
new Person{FirstName="shan",LastName="yongxu"},
new Person{FirstName="sun",LastName="yanzhao"},
new Person{FirstName="zhu",LastName="haitao"},
new Person{FirstName="wang",LastName="jiwei"}
};
Array.Sort(persons,new PersonComparer(PersonCompareType.FirstName));
foreach (var item in persons)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
public class Person : IComparable<Person>
{
public Person()
{ }
public Person(string firstName, string lastName)
{
this.FirstName = firstName;
LastName = lastName;
}
public string FirstName { get; set; }
public string LastName { get; set; }
public override string ToString()
{
return String.Format("{0} {1}", FirstName, LastName);
}
public int CompareTo(Person other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
int result = this.LastName.CompareTo(other.LastName);
if (result == 0)
{
result = this.FirstName.CompareTo(other.FirstName);
}
return result;
}
}
}
分析:类PersonComparer实现了IComparer<Person>接口,可以按照FirstName或LastName对象排序.枚举PersonComparerType定义了可用于PersonComparer的排序选项:FirstName和LastName.排序方式由PersonComparer类的构造函数定义,在该构造函数中设置了一个PersonComparerType值.实现Compare()方法时用一个switch语句指定是按照FirstName还是LastName排序.
这样就可以按照你的需要来排序;是按照姓排序,还是按照名排序?
Array类还提供了Sort方法,需要将一个委托作为参数,这个参数可以传递给方法,从而比较两个对象,而不需要依赖IComparable或IComparer接口.