C#常用算法帖:数组

原文链接 http://blog.csdn.net/ghostbear/article/details/8702045

 C#对数组概念进行了彻底的面向对象化,很大程度上降低了我们对数组结构的使用难度,并且它已经支持了.Net平台的垃圾收集机制。随着C#版本的不断更新,从数组中派生出的新数据结构也日益增加。按照28原理,我们只需要使用其中的20%就能解决80%的问题。但为了做到极致,我们还是需要了解下它们。本文总结到C#(4.0)为止的一些与数组相关的数据结构以及它们的用法。

基本数组
[csharp]  view plain  copy
 print ?
  1. string[] fruit = new string[5];  
  2. string[] vegetable = new string[] { "chinese cabbage""pepper""potato""tomato""broccoli" };  
  3. fruit[0] = "orange";  
  4. fruit[1] = "banana";  
  5. fruit[2] = "apple";  
  6. fruit[3] = "grape";  
  7. fruit[4] = "lychee";  

多维数组
[csharp]  view plain  copy
 print ?
  1. string[,] monthplan=new string[30,7];  
  2. monthplan[0,0]="A";  
  3. monthplan[0, 1] = "B";  

锯齿数组
[csharp]  view plain  copy
 print ?
  1. string[][] foodenum = new string[7][];  
  2. foodenum[0] = new string[10];  
  3. foodenum[1] = new string[9];  
  4. foodenum[2] = new string[8];  
  5. foodenum[3] = new string[7];  
  6. foodenum[4] = new string[6];  
  7. foodenum[5] = new string[5];  
  8. foodenum[6] = new string[6];  

IEnumerator接口
[csharp]  view plain  copy
 print ?
  1. public static IEnumerator<string> Yield()  
  2. {  
  3.     yield return "apple";  
  4.     yield return "orange";  
  5.     yield return "banana";  
  6. }  
  7.   
  8. public static void IEnumeratorTest()  
  9. {  
  10.     var iterator=Yield();  
  11.     while (iterator.MoveNext())  
  12.     {  
  13.         Console.WriteLine(iterator.Current);  
  14.     }  
  15.     /*OUT PUT 
  16.      apple 
  17.      orange 
  18.      banana 
  19.      */  
  20. }  
数组片断
[csharp]  view plain  copy
 print ?
  1. public static void ArraySegment()  
  2. {  
  3.     string[] vegetable = new string[] { "chinese cabbage""pepper""potato""tomato""broccoli" };  
  4.     // count: get data begin the current index.  
  5.     ArraySegment<string> arraySegment = new ArraySegment<string>(vegetable,2,2);  
  6.     for (int i = arraySegment.Offset; i <= arraySegment.Offset + arraySegment.Count; i++)  
  7.     {  
  8.         Console.WriteLine(arraySegment.Array[i]);  
  9.     }  
  10.     /* 
  11.      * OUT PUT 
  12.      * potato 
  13.        tomato 
  14.        broccoli 
  15.      */  
  16. }  
元组
[csharp]  view plain  copy
 print ?
  1. public static void Tuples()  
  2. {   
  3.     var tuple=Tuple.Create<string,string,string,string,int,int,int,Tuple<double,double>>("A"  
  4.                                                                                          ,"B"  
  5.                                                                                          ,"C"  
  6.                                                                                          ,"D"  
  7.                                                                                          ,1  
  8.                                                                                          ,2  
  9.                                                                                          ,3  
  10.                                                                                          ,Tuple.Create<double,double>(11.1,22.2));  
  11.     Console.WriteLine(tuple.ToString());  
  12.     Console.WriteLine(tuple.Item1);  
  13.     Console.WriteLine(tuple.Item2);  
  14.     Console.WriteLine(tuple.Item3);  
  15.     Console.WriteLine(tuple.Item4);  
  16.     Console.WriteLine(tuple.Item5);  
  17.     Console.WriteLine(tuple.Item6);  
  18.     Console.WriteLine(tuple.Item7);  
  19.     Console.WriteLine(tuple.Rest.Item1.Item1);  
  20.     Console.WriteLine(tuple.Rest.Item1.Item2);  
  21.   
  22.     /*OUT PUT 
  23.      (A, B, C, D, 1, 2, 3, (11.1, 22.2)) 
  24.     A 
  25.     B 
  26.     C 
  27.     D 
  28.     1 
  29.     2 
  30.     3 
  31.     11.1 
  32.     22.2 
  33.  
  34.      */  
  35.   
  36. }  

 

协变
[csharp]  view plain  copy
 print ?
  1. public static void ArrayConvertTest()  
  2. {  
  3.     string[] vegetable = new string[] { "chinese cabbage""pepper""potato""tomato""broccoli" };  
  4.     object[] copyVegetable = vegetable;  
  5.     foreach (object item in copyVegetable)  
  6.     {  
  7.         Console.WriteLine(item);  
  8.     }  
  9.     /* 
  10.         OUT PUT 
  11.         chinese cabbage 
  12.         pepper 
  13.         potato 
  14.         tomato 
  15.         broccoli 
  16.  
  17.      */  
  18. }  


 

IStructuralEquatable 和 IEqualityComparer

    这两接口可以比较数组中每个元素是否与另外一个数组中的元素中的等值性,IEqualityComparer是比较器,决定元素的比较方式。

[csharp]  view plain  copy
 print ?
  1. class Person : IEquatable<Person>  
  2. {  
  3.     public string firstName, lastName;  
  4.     public int age;  
  5.     public Person(string firstName, string lastName, int age)  
  6.     {  
  7.         this.firstName = firstName;  
  8.         this.lastName = lastName;  
  9.         this.age = age;  
  10.     }  
  11.   
  12.     public bool Equals(Person other)  
  13.     {  
  14.         return this.firstName + this.lastName == other.firstName + other.lastName;  
  15.     }  
  16.   
  17. }  
  18.   
  19. class PersonEqualComparer : IEqualityComparer  
  20. {  
  21.   
  22.   
  23.   
  24.     public bool Equals(object x, object y)  
  25.     {  
  26.         Person p1 = x as Person;  
  27.         Person p2 = y as Person;  
  28.         if (null == p1 || null == p2)  
  29.         {  
  30.             return false;  
  31.         }  
  32.         return p1.firstName == p2.firstName && p1.lastName == p2.lastName && p1.age == p2.age;  
  33.     }  
  34.   
  35.     public int GetHashCode(object obj)  
  36.     {  
  37.         throw new NotImplementedException();  
  38.     }  
  39. }  
  40.   
  41.     /// <summary>  
  42.     /// this interface can use to compare value-equalization in two different arraies or tupes.  
  43.     /// </summary>  
  44.     public static void IStructuralEquatableTest()  
  45.     {  
  46.         Person[] family1 = new Person[]  
  47.         {  
  48.             new Person("jim","green",21)  
  49.             ,new Person("lucy","li",22)  
  50.             ,new Person ("jack","wang",23)  
  51.         };  
  52.   
  53.         Person[] family2 = new Person[]  
  54.         {  
  55.             new Person("jim","green",21)  
  56.             ,new Person("lucy","li",22)  
  57.             ,new Person ("jack","wang",22)  
  58.         };  
  59.   
  60.           
  61.   
  62.         if (family1 == family2)  
  63.         {  
  64.             Console.WriteLine("it's equal");  
  65.         }  
  66.         else  
  67.         {  
  68.             Console.WriteLine("it's not equal");  
  69.         }  
  70.   
  71.         if ((family1 as IStructuralEquatable).Equals(family2, EqualityComparer<Person>.Default))  
  72.         {  
  73.             Console.WriteLine("it's equal");  
  74.         }  
  75.         else  
  76.         {  
  77.             Console.WriteLine("it's not equal");  
  78.         }  
  79.   
  80.   
  81.         if ((family1 as IStructuralEquatable).Equals(family2, new PersonEqualComparer()))  
  82.         {  
  83.             Console.WriteLine("it's equal");  
  84.         }  
  85.         else  
  86.         {  
  87.             Console.WriteLine("it's not equal");  
  88.         }  
  89.   
  90.         /* OUT PUT 
  91.          * it's not equal 
  92.          * it's equal 
  93.          * it's not equal 
  94.          */  
  95.     }  
BitArray
[csharp]  view plain  copy
 print ?
  1. public static void TestBitArray()  
  2.        {  
  3.            BitArray myBitArray = new BitArray(8);// set the length of the array  
  4.            myBitArray.Set(0, true);//current value 10000000  
  5.            myBitArray.Set(7, true);//current value 00000001  
  6.            DisplayBit(myBitArray);  
  7.   
  8.            BitArray myBitArray2 = new BitArray(8);  
  9.            myBitArray2.Set(0, true);//current vlaue 10000000  
  10.            myBitArray2.Set(1, true);//current value 11000000  
  11.            DisplayBit(myBitArray2);  
  12.   
  13.            Console.WriteLine("Not");  
  14.            myBitArray.Not();  
  15.            DisplayBit(myBitArray);  
  16.   
  17.            Console.WriteLine("And");  
  18.            myBitArray.And(myBitArray2);  
  19.            DisplayBit(myBitArray);  
  20.   
  21.   
  22.            Console.WriteLine("Xor");  
  23.            myBitArray.Xor(myBitArray2);  
  24.            DisplayBit(myBitArray);  
  25.   
  26.            /*OUT PUT 
  27.                10000001 
  28.                11000000 
  29.                Not 
  30.                01111110 
  31.                And 
  32.                01000000 
  33.                Xor 
  34.                10000000 
  35.  
  36.             */  
  37.   
  38.        }  


Array成员列表
  1. Name:AsReadOnly Type:Method
  2. Name:Resize Type:Method
  3. Name:CreateInstance Type:Method
  4. Name:CreateInstance Type:Method
  5. Name:CreateInstance Type:Method
  6. Name:CreateInstance Type:Method
  7. Name:CreateInstance Type:Method
  8. Name:CreateInstance Type:Method
  9. Name:Copy Type:Method
  10. Name:Copy Type:Method
  11. Name:ConstrainedCopy Type:Method
  12. Name:Copy Type:Method
  13. Name:Copy Type:Method
  14. Name:Clear Type:Method
  15. Name:GetValue Type:Method
  16. Name:GetValue Type:Method
  17. Name:GetValue Type:Method
  18. Name:GetValue Type:Method
  19. Name:GetValue Type:Method
  20. Name:GetValue Type:Method
  21. Name:GetValue Type:Method
  22. Name:GetValue Type:Method
  23. Name:SetValue Type:Method
  24. Name:SetValue Type:Method
  25. Name:SetValue Type:Method
  26. Name:SetValue Type:Method
  27. Name:SetValue Type:Method
  28. Name:SetValue Type:Method
  29. Name:SetValue Type:Method
  30. Name:SetValue Type:Method
  31. Name:get_Length Type:Method
  32. Name:get_LongLength Type:Method
  33. Name:GetLength Type:Method
  34. Name:GetLongLength Type:Method
  35. Name:get_Rank Type:Method
  36. Name:GetUpperBound Type:Method
  37. Name:GetLowerBound Type:Method
  38. Name:get_SyncRoot Type:Method
  39. Name:get_IsReadOnly Type:Method
  40. Name:get_IsFixedSize Type:Method
  41. Name:get_IsSynchronized Type:Method
  42. Name:Clone Type:Method
  43. Name:BinarySearch Type:Method
  44. Name:BinarySearch Type:Method
  45. Name:BinarySearch Type:Method
  46. Name:BinarySearch Type:Method
  47. Name:BinarySearch Type:Method
  48. Name:BinarySearch Type:Method
  49. Name:BinarySearch Type:Method
  50. Name:BinarySearch Type:Method
  51. Name:ConvertAll Type:Method
  52. Name:CopyTo Type:Method
  53. Name:CopyTo Type:Method
  54. Name:Exists Type:Method
  55. Name:Find Type:Method
  56. Name:FindAll Type:Method
  57. Name:FindIndex Type:Method
  58. Name:FindIndex Type:Method
  59. Name:FindIndex Type:Method
  60. Name:FindLast Type:Method
  61. Name:FindLastIndex Type:Method
  62. Name:FindLastIndex Type:Method
  63. Name:FindLastIndex Type:Method
  64. Name:ForEach Type:Method
  65. Name:GetEnumerator Type:Method
  66. Name:IndexOf Type:Method
  67. Name:IndexOf Type:Method
  68. Name:IndexOf Type:Method
  69. Name:IndexOf Type:Method
  70. Name:IndexOf Type:Method
  71. Name:IndexOf Type:Method
  72. Name:LastIndexOf Type:Method
  73. Name:LastIndexOf Type:Method
  74. Name:LastIndexOf Type:Method
  75. Name:LastIndexOf Type:Method
  76. Name:LastIndexOf Type:Method
  77. Name:LastIndexOf Type:Method
  78. Name:Reverse Type:Method
  79. Name:Reverse Type:Method
  80. Name:Sort Type:Method
  81. Name:Sort Type:Method
  82. Name:Sort Type:Method
  83. Name:Sort Type:Method
  84. Name:Sort Type:Method
  85. Name:Sort Type:Method
  86. Name:Sort Type:Method
  87. Name:Sort Type:Method
  88. Name:Sort Type:Method
  89. Name:Sort Type:Method
  90. Name:Sort Type:Method
  91. Name:Sort Type:Method
  92. Name:Sort Type:Method
  93. Name:Sort Type:Method
  94. Name:Sort Type:Method
  95. Name:Sort Type:Method
  96. Name:Sort Type:Method
  97. Name:TrueForAll Type:Method
  98. Name:Initialize Type:Method
  99. Name:ToString Type:Method
  100. Name:Equals Type:Method
  101. Name:GetHashCode Type:Method
  102. Name:GetType Type:Method
  103. Name:Length Type:Property
  104. Name:LongLength Type:Property
  105. Name:Rank Type:Property
  106. Name:SyncRoot Type:Property
  107. Name:IsReadOnly Type:Property
  108. Name:IsFixedSize Type:Property
  109. Name:IsSynchronized Type:Property

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值