原文链接 http://blog.csdn.net/ghostbear/article/details/8702045
C#对数组概念进行了彻底的面向对象化,很大程度上降低了我们对数组结构的使用难度,并且它已经支持了.Net平台的垃圾收集机制。随着C#版本的不断更新,从数组中派生出的新数据结构也日益增加。按照28原理,我们只需要使用其中的20%就能解决80%的问题。但为了做到极致,我们还是需要了解下它们。本文总结到C#(4.0)为止的一些与数组相关的数据结构以及它们的用法。
基本数组
- string[] fruit = new string[5];
- string[] vegetable = new string[] { "chinese cabbage", "pepper", "potato", "tomato", "broccoli" };
- fruit[0] = "orange";
- fruit[1] = "banana";
- fruit[2] = "apple";
- fruit[3] = "grape";
- fruit[4] = "lychee";
多维数组
- string[,] monthplan=new string[30,7];
- monthplan[0,0]="A";
- monthplan[0, 1] = "B";
锯齿数组
- string[][] foodenum = new string[7][];
- foodenum[0] = new string[10];
- foodenum[1] = new string[9];
- foodenum[2] = new string[8];
- foodenum[3] = new string[7];
- foodenum[4] = new string[6];
- foodenum[5] = new string[5];
- foodenum[6] = new string[6];
IEnumerator接口
- public static IEnumerator<string> Yield()
- {
- yield return "apple";
- yield return "orange";
- yield return "banana";
- }
-
- public static void IEnumeratorTest()
- {
- var iterator=Yield();
- while (iterator.MoveNext())
- {
- Console.WriteLine(iterator.Current);
- }
-
-
-
-
-
- }
数组片断
- public static void ArraySegment()
- {
- string[] vegetable = new string[] { "chinese cabbage", "pepper", "potato", "tomato", "broccoli" };
-
- ArraySegment<string> arraySegment = new ArraySegment<string>(vegetable,2,2);
- for (int i = arraySegment.Offset; i <= arraySegment.Offset + arraySegment.Count; i++)
- {
- Console.WriteLine(arraySegment.Array[i]);
- }
-
-
-
-
-
-
- }
元组
- public static void Tuples()
- {
- var tuple=Tuple.Create<string,string,string,string,int,int,int,Tuple<double,double>>("A"
- ,"B"
- ,"C"
- ,"D"
- ,1
- ,2
- ,3
- ,Tuple.Create<double,double>(11.1,22.2));
- Console.WriteLine(tuple.ToString());
- Console.WriteLine(tuple.Item1);
- Console.WriteLine(tuple.Item2);
- Console.WriteLine(tuple.Item3);
- Console.WriteLine(tuple.Item4);
- Console.WriteLine(tuple.Item5);
- Console.WriteLine(tuple.Item6);
- Console.WriteLine(tuple.Item7);
- Console.WriteLine(tuple.Rest.Item1.Item1);
- Console.WriteLine(tuple.Rest.Item1.Item2);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
协变
- public static void ArrayConvertTest()
- {
- string[] vegetable = new string[] { "chinese cabbage", "pepper", "potato", "tomato", "broccoli" };
- object[] copyVegetable = vegetable;
- foreach (object item in copyVegetable)
- {
- Console.WriteLine(item);
- }
-
-
-
-
-
-
-
-
-
- }
IStructuralEquatable 和 IEqualityComparer
这两接口可以比较数组中每个元素是否与另外一个数组中的元素中的等值性,IEqualityComparer是比较器,决定元素的比较方式。
- class Person : IEquatable<Person>
- {
- public string firstName, lastName;
- public int age;
- public Person(string firstName, string lastName, int age)
- {
- this.firstName = firstName;
- this.lastName = lastName;
- this.age = age;
- }
-
- public bool Equals(Person other)
- {
- return this.firstName + this.lastName == other.firstName + other.lastName;
- }
-
- }
-
- class PersonEqualComparer : IEqualityComparer
- {
-
-
-
- public bool Equals(object x, object y)
- {
- Person p1 = x as Person;
- Person p2 = y as Person;
- if (null == p1 || null == p2)
- {
- return false;
- }
- return p1.firstName == p2.firstName && p1.lastName == p2.lastName && p1.age == p2.age;
- }
-
- public int GetHashCode(object obj)
- {
- throw new NotImplementedException();
- }
- }
-
-
-
-
- public static void IStructuralEquatableTest()
- {
- Person[] family1 = new Person[]
- {
- new Person("jim","green",21)
- ,new Person("lucy","li",22)
- ,new Person ("jack","wang",23)
- };
-
- Person[] family2 = new Person[]
- {
- new Person("jim","green",21)
- ,new Person("lucy","li",22)
- ,new Person ("jack","wang",22)
- };
-
-
-
- if (family1 == family2)
- {
- Console.WriteLine("it's equal");
- }
- else
- {
- Console.WriteLine("it's not equal");
- }
-
- if ((family1 as IStructuralEquatable).Equals(family2, EqualityComparer<Person>.Default))
- {
- Console.WriteLine("it's equal");
- }
- else
- {
- Console.WriteLine("it's not equal");
- }
-
-
- if ((family1 as IStructuralEquatable).Equals(family2, new PersonEqualComparer()))
- {
- Console.WriteLine("it's equal");
- }
- else
- {
- Console.WriteLine("it's not equal");
- }
-
-
-
-
-
-
- }
BitArray
- public static void TestBitArray()
- {
- BitArray myBitArray = new BitArray(8);
- myBitArray.Set(0, true);
- myBitArray.Set(7, true);
- DisplayBit(myBitArray);
-
- BitArray myBitArray2 = new BitArray(8);
- myBitArray2.Set(0, true);
- myBitArray2.Set(1, true);
- DisplayBit(myBitArray2);
-
- Console.WriteLine("Not");
- myBitArray.Not();
- DisplayBit(myBitArray);
-
- Console.WriteLine("And");
- myBitArray.And(myBitArray2);
- DisplayBit(myBitArray);
-
-
- Console.WriteLine("Xor");
- myBitArray.Xor(myBitArray2);
- DisplayBit(myBitArray);
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
Array成员列表
- Name:AsReadOnly Type:Method
- Name:Resize Type:Method
- Name:CreateInstance Type:Method
- Name:CreateInstance Type:Method
- Name:CreateInstance Type:Method
- Name:CreateInstance Type:Method
- Name:CreateInstance Type:Method
- Name:CreateInstance Type:Method
- Name:Copy Type:Method
- Name:Copy Type:Method
- Name:ConstrainedCopy Type:Method
- Name:Copy Type:Method
- Name:Copy Type:Method
- Name:Clear Type:Method
- Name:GetValue Type:Method
- Name:GetValue Type:Method
- Name:GetValue Type:Method
- Name:GetValue Type:Method
- Name:GetValue Type:Method
- Name:GetValue Type:Method
- Name:GetValue Type:Method
- Name:GetValue Type:Method
- Name:SetValue Type:Method
- Name:SetValue Type:Method
- Name:SetValue Type:Method
- Name:SetValue Type:Method
- Name:SetValue Type:Method
- Name:SetValue Type:Method
- Name:SetValue Type:Method
- Name:SetValue Type:Method
- Name:get_Length Type:Method
- Name:get_LongLength Type:Method
- Name:GetLength Type:Method
- Name:GetLongLength Type:Method
- Name:get_Rank Type:Method
- Name:GetUpperBound Type:Method
- Name:GetLowerBound Type:Method
- Name:get_SyncRoot Type:Method
- Name:get_IsReadOnly Type:Method
- Name:get_IsFixedSize Type:Method
- Name:get_IsSynchronized Type:Method
- Name:Clone Type:Method
- Name:BinarySearch Type:Method
- Name:BinarySearch Type:Method
- Name:BinarySearch Type:Method
- Name:BinarySearch Type:Method
- Name:BinarySearch Type:Method
- Name:BinarySearch Type:Method
- Name:BinarySearch Type:Method
- Name:BinarySearch Type:Method
- Name:ConvertAll Type:Method
- Name:CopyTo Type:Method
- Name:CopyTo Type:Method
- Name:Exists Type:Method
- Name:Find Type:Method
- Name:FindAll Type:Method
- Name:FindIndex Type:Method
- Name:FindIndex Type:Method
- Name:FindIndex Type:Method
- Name:FindLast Type:Method
- Name:FindLastIndex Type:Method
- Name:FindLastIndex Type:Method
- Name:FindLastIndex Type:Method
- Name:ForEach Type:Method
- Name:GetEnumerator Type:Method
- Name:IndexOf Type:Method
- Name:IndexOf Type:Method
- Name:IndexOf Type:Method
- Name:IndexOf Type:Method
- Name:IndexOf Type:Method
- Name:IndexOf Type:Method
- Name:LastIndexOf Type:Method
- Name:LastIndexOf Type:Method
- Name:LastIndexOf Type:Method
- Name:LastIndexOf Type:Method
- Name:LastIndexOf Type:Method
- Name:LastIndexOf Type:Method
- Name:Reverse Type:Method
- Name:Reverse Type:Method
- Name:Sort Type:Method
- Name:Sort Type:Method
- Name:Sort Type:Method
- Name:Sort Type:Method
- Name:Sort Type:Method
- Name:Sort Type:Method
- Name:Sort Type:Method
- Name:Sort Type:Method
- Name:Sort Type:Method
- Name:Sort Type:Method
- Name:Sort Type:Method
- Name:Sort Type:Method
- Name:Sort Type:Method
- Name:Sort Type:Method
- Name:Sort Type:Method
- Name:Sort Type:Method
- Name:Sort Type:Method
- Name:TrueForAll Type:Method
- Name:Initialize Type:Method
- Name:ToString Type:Method
- Name:Equals Type:Method
- Name:GetHashCode Type:Method
- Name:GetType Type:Method
- Name:Length Type:Property
- Name:LongLength Type:Property
- Name:Rank Type:Property
- Name:SyncRoot Type:Property
- Name:IsReadOnly Type:Property
- Name:IsFixedSize Type:Property
- Name:IsSynchronized Type:Property