Welcome

首页 / 软件开发 / 数据结构与算法 / C#常用算法:数组

C#常用算法:数组2013-11-14C#对数组概念进行了彻底的面向对象化,很大程度上降低了我们对数组结构的使用难度,并且它已经支持了.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);}/*OUT PUTappleorangebanana*/}
数组片断

public static void ArraySegment(){string[] vegetable = new string[] { "chinese cabbage", "pepper", "potato", "tomato", "broccoli" };// count: get data begin the current index.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]);}/** OUT PUT* potatotomatobroccoli*/}