关闭

C#中不常用的数据结构

标签: 数据结构c#stringtablerandomobject
1966人阅读 评论(0) 收藏 举报

转自:http://blog.csdn.net/ghostbear/article/details/7317786

 

锯齿数组           

  1. ////从1到9打印出一个三角状的图像 
  2.  
  3.             int[][] table = new int[9][]; 
  4.  
  5.             for (int i = 0; i <= table.GetUpperBound(0); i++) 
  6.  
  7.             { 
  8.  
  9.                 table[i] = new int[i + 1]; 
  10.  
  11.                 for (int j = 0; j <= table[i].GetUpperBound(0); j++) 
  12.  
  13.                 { 
  14.  
  15.                     table[i][j] = j + 1; 
  16.  
  17.                 } 
  18.  
  19.   
  20.  
  21.             } 
  22.  
  23.   
  24.  
  25.             for (int i = 0; i <= table.GetUpperBound(0); i++) 
  26.  
  27.             { 
  28.  
  29.                 for (int j = 0; j <=table[i].GetUpperBound(0); j++) 
  30.  
  31.                 { 
  32.  
  33.                     Console.Write(table[i][j].ToString()+" "); 
  34.  
  35.                 } 
  36.  
  37.                 Console.WriteLine(); 
  38.  
  39.             } 

数组片段          

  1. //ArraySegment(数组片段) 
  2.  
  3.            /*
  4.            int[] data=new int[]{1,2,3,4,5,6,7,8,9};
  5.            ArraySegment<int> segment = new ArraySegment<int>(data, 4, 5);
  6.            for (int i = 0; i < segment.Count; i++)
  7.            {
  8.                Console.Write(segment.Array[segment.Offset + i].ToString()
  9. + " ");
  10.            }
  11.            Console.WriteLine();
  12.            */ 

IStructuralEquatable和IstructuralComparable

  1. //比较数组的等值性 ,被比较的元素需要实现接口IEuqatable 
  2.  
  3. Person[] family1 = new Person[] 
  4.  
  5.             { 
  6.  
  7.                 new Person("jim","green",21) 
  8.  
  9.                 ,new Person("lucy","li",22) 
  10.  
  11.                 ,new Person ("jack","wang",23) 
  12.  
  13.             }; 
  14.  
  15.   
  16.  
  17.             Person[] family2 = new Person[] 
  18.  
  19.             { 
  20.  
  21.                 new Person("jim","green",21) 
  22.  
  23.                 ,new Person("lucy","li",22) 
  24.  
  25.                 ,new Person ("jack","wang",23) 
  26.  
  27.             }; 
  28.  
  29.   
  30.  
  31.             if (family1 == family2) 
  32.  
  33.             { 
  34.  
  35.                 Console.WriteLine("it's equal"); 
  36.  
  37.             } 
  38.  
  39.             else 
  40.  
  41.             { 
  42.  
  43.                 Console.Write("it's not equal"); 
  44.  
  45.             } 
  46.  
  47.   
  48.  
  49.             if ((family1 as IStructuralEquatable).Equals(family2, EqualityComparer<Person>.Default)) 
  50.  
  51.             { 
  52.  
  53.                 Console.WriteLine("it's equal"); 
  54.  
  55.             } 
  56.  
  57.             else 
  58.  
  59.             { 
  60.  
  61.                 Console.Write("it's not equal"); 
  62.  
  63.             } 
  64.   

IFormatProvider

  1. //对象必须实现接口Iformattable 
  2.  
  3. class Person:IFormattable,IComparable<Person> 
  4.  
  5.     { 
  6.  
  7.         public string FirstName { get; set; } 
  8.  
  9.         public string LastName { get; set; } 
  10.  
  11.         public int Age { get; set; } 
  12.  
  13.         public string ToString(string format, IFormatProvider formatProvider) 
  14.  
  15.         { 
  16.  
  17.             string result = string.Empty; 
  18.  
  19.             switch (format) 
  20.  
  21.             {  
  22.  
  23.                 case "F"
  24.  
  25.                     result = FirstName; 
  26.  
  27.                     break
  28.  
  29.                 case "L"
  30.  
  31.                     result = LastName; 
  32.  
  33.                     break
  34.  
  35.                 case "FULL"
  36.  
  37.                     result = FirstName + LastName; 
  38.  
  39.                     break
  40.  
  41.                 default
  42.  
  43.                     break
  44.  
  45.   
  46.  
  47.             } 
  48.  
  49.             return result; 
  50.  
  51.         } 
  52.  
  53.   
  54.  
  55.         public int CompareTo(Person other) 
  56.  
  57.         { 
  58.  
  59.             return this.FirstName.CompareTo(other.FirstName); 
  60.  
  61.         } 
  62.  
  63.     } 


调用代码:

  1. Person jim = new Person() { FirstName = "Jim", LastName = "Green", Age = 11 }; 
  2.  
  3. Console.WriteLine(jim.ToString("F", null)); 

SortedList(有序列表)

  1. SortedList<string, string> charaterList = new SortedList<string, string>(); 
  2.  
  3. charaterList.Add("A", "this is charater A"); 
  4.  
  5. charaterList.Add("E", "this is charater E"); 
  6.  
  7. charaterList.Add("D", "this is charater D"); 
  8.  
  9. charaterList.Add("C", "this is charater C"); 
  10.  
  11. charaterList.Add("B", "this is charater B"); 
  12.  
  13.  
  14.  
  15. foreach(string tmp in charaterList.Keys) 
  16.  
  17.  
  18.     Console.WriteLine(charaterList[tmp]); 
  19.  

执行结果:

 

this is charater A

this is charater B

this is charater C

this is charater D

this is charater E

字典

  1. Dictionary<string, string> programBook = new Dictionary<string, string>(); 
  2.  
  3. programBook.Add("chapter1", "base programming skill"); 
  4.  
  5. programBook.Add("chapter2", "the amateurism programming skill"); 
  6.  
  7. programBook.Add("chapter3", "the professional programming skill"); 
  8.  
  9. programBook.Add("chapter4", "the god programming skill"); 
  10.  
  11.  
  12.  
  13. Random rand = new Random(); 
  14.  
  15. string[] content = programBook.Keys.ToArray<string>(); 
  16.  
  17.  
  18.  
  19. Console.WriteLine(programBook[content[rand.Next(0, 4)]]); 


Lookup(一键多值)

  1. Person[] persons = new Person[]{ 
  2.  
  3.                new Person{FirstName="Li",LastName="Ming",Age=21}, 
  4.  
  5.                new Person{FirstName="Li",LastName="XinLiang",Age=22}, 
  6.  
  7.                new Person{FirstName="Wang",LastName="Kai",Age=23}, 
  8.  
  9.                new Person{FirstName="Li",LastName="SiMing",Age=24} 
  10.  
  11.    }; 
  12.  
  13.  
  14.  
  15.    var personsContent = persons.ToLookup(person => person.FirstName); 
  16.  
  17.    ILookup<string,Person> ok = persons.ToLookup<Person, string>(a => a.FirstName); 
  18.  
  19.  
  20.  
  21.    foreach (Person tmp in personsContent["Li"]) 
  22.  
  23.    { 
  24.  
  25.        Console.WriteLine(tmp.FirstName+tmp.LastName); 
  26.  
  27.    } 
  28.  
  29.  
  30.  
  31.    foreach (Person tmp in ok["Li"]) 
  32.  
  33.    { 
  34.  
  35.        Console.WriteLine(tmp.FirstName + tmp.LastName); 
  36.  
  37.    } 


结果:

 

LiMing

LiXinLiang

LiSiMing

LiMing

LiXinLiang

LiSiMing

SortedDictionary(有序字典)

  1. SortedDictionary<string, string> book = new SortedDictionary<string, string>(); 
  2.  
  3.     book.Add("chapter1", "this is chapter 1"); 
  4.  
  5.     book.Add("chapter6", "this is chapter 6"); 
  6.  
  7.     book.Add("chapter3", "this is chapter 3"); 
  8.  
  9.     book.Add("chapter2", "this is chapter 2"); 
  10.  
  11.     book.Add("chapter9", "this is chapter 9"); 
  12.  
  13.     book.Add("chapter4", "this is chapter 4"); 
  14.  
  15.     book.Add("chapter7", "this is chapter 7"); 
  16.  
  17.     book.Add("chapter5", "this is chapter 5"); 
  18.  
  19.     book.Add("chapter8", "this is chapter 8"); 
  20.  
  21.             
  22.  
  23.   
  24.  
  25.     foreach(string key in book.Keys) 
  26.  
  27.     { 
  28.  
  29.         Console.WriteLine(key + ":" + book[key]); 
  30.  
  31.     } 
  32.  
  33.   
  34.  
  35. Console.WriteLine(); 
  36.  
  37.   
  38.  
  39.   
  40.  
  41.     SortedDictionary<Person, string> contactBook = new SortedDictionary<Person, string>(); 
  42.  
  43.     contactBook.Add(new Person { FirstName = "Li", LastName = "XiaoYao", Age = 20 }, "135****3797"); 
  44.  
  45.     contactBook.Add(new Person { FirstName = "Zhao", LastName = "LinEr", Age = 20 }, "132****8534"); 
  46.  
  47.     contactBook.Add(new Person { FirstName = "Lin", LastName = "YueRu", Age = 20 }, "131****6734"); 
  48.  
  49.     contactBook.Add(new Person { FirstName = "Jiu", LastName = "Xian", Age = 20 }, "137****3215"); 
  50.  
  51.     contactBook.Add(new Person { FirstName = "Xiao", LastName = "QiGai", Age = 20 }, "134****4751"); 
  52.  
  53.     contactBook.Add(new Person { FirstName = "Jian", LastName = "Xian", Age = 20 }, "133****1642"); 
  54.  
  55.   
  56.  
  57.      
  58.  
  59.     foreach (Person key in contactBook.Keys) 
  60.  
  61.     { 
  62.  
  63.         Console.WriteLine(key.FirstName+key.LastName + ":" + contactBook[key]); 
  64.  
  65.     } 


执行结果:

 

chapter1:this is chapter 1

chapter2:this is chapter 2

chapter3:this is chapter 3

chapter4:this is chapter 4

chapter5:this is chapter 5

chapter6:this is chapter 6

chapter7:this is chapter 7

chapter8:this is chapter 8

chapter9:this is chapter 9

JianXian:133****1642

JiuXian:137****3215

LiXiaoYao:135****3797

LinYueRu:131****6734

XiaoQiGai:134****4751

ZhaoLinEr:132****8534

Iset接口(HashSet,SortedSet)

    Iset接口提供如下方法:创建合集,创建交集,或于一个集合做比较判断是否为其的子集或超集。

  1. //无序集合HashSet 
  2. HashSet<string> charaterList = new HashSet<string>(); 
  3. charaterList.Add("A"); 
  4. charaterList.Add("B"); 
  5. charaterList.Add("C"); 
  6. if (!charaterList.Add("A")) 
  7.     Console.WriteLine("Current Collection already have the 'A'"); 
  8. foreach (string tmp in charaterList) 
  9.     Console.WriteLine(tmp); 
  10. Console.WriteLine(); 
  11.         
  12.  
  13.  
  14. //有序集合SortedSet 
  15. SortedSet<string> charaterList2 = new SortedSet<string>(); 
  16. charaterList2.Add("A"); 
  17. charaterList2.Add("B"); 
  18. charaterList2.Add("D"); 
  19. charaterList2.Add("C"); 
  20. charaterList2.Add("G"); 
  21. charaterList2.Add("E"); 
  22. charaterList2.Add("F"); 
  23. if (!charaterList.Add("A")) 
  24.     Console.WriteLine("Current Collection already have the 'A'"); 
  25.  
  26. foreach (string tmp in charaterList2) 
  27.     Console.WriteLine(tmp); 
  28. //IsSubSetOf()和IsSuperSetOf()方法 
  29. // int[] onetoten = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 
  30. //int[] fivetoeight = new int[] { 5, 6, 7, 8 }; 
  31.          
  32.  
  33. HashSet<int> onetoten = new HashSet<int>(); 
  34. onetoten.Add(1); 
  35. onetoten.Add(2); 
  36. onetoten.Add(3); 
  37. onetoten.Add(4); 
  38. onetoten.Add(5); 
  39. onetoten.Add(6); 
  40. onetoten.Add(7); 
  41. onetoten.Add(8); 
  42. onetoten.Add(9); 
  43. onetoten.Add(10); 
  44.  
  45. HashSet<int> fivetoeight = new HashSet<int>(); 
  46. fivetoeight.Add(5); 
  47. fivetoeight.Add(6); 
  48. fivetoeight.Add(7); 
  49. fivetoeight.Add(8); 
  50.  
  51. HashSet<int> eleventofifteen = new HashSet<int>(); 
  52. eleventofifteen.Add(11); 
  53. eleventofifteen.Add(12); 
  54. eleventofifteen.Add(13); 
  55. eleventofifteen.Add(14); 
  56. eleventofifteen.Add(15); 
  57. eleventofifteen.Add(1); 
  58.  
  59.         
  60.  
  61. if (fivetoeight.IsSubsetOf(onetoten)) 
  62.     Console.WriteLine("fivetoeight is subset of onetoten."); 
  63.  
  64. if (fivetoeight.IsSupersetOf(fivetoeight)) 
  65.     Console.WriteLine("ontoten is superset of fivetoten"); 
  66.  
  67. //onetoten.UnionWith(eleventofifteen);//这里包含重复的元素 
  68. onetoten.ExceptWith(fivetoeight); 
  69. foreach (int tmp in onetoten) 
  70.     Console.WriteLine(tmp); 


执行结果:

 

Current Collection already have the 'A'

A

B

C

Current Collection already have the 'A'

A

B

C

D

E

F

G

fivetoeight is subset of onetoten.

ontoten is superset of fivetoten

1

2

3

4

9

10

可观察的集合

  1. ObservableCollection<string> paperFirm = new ObservableCollection<string>(); 
  2.  
  3.     string[] personList=new string[]{"Bank","Bruce"}; 
  4.  
  5.   
  6.  
  7.     paperFirm.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(paperFirm_CollectionChanged); 
  8.  
  9.   
  10.  
  11.     paperFirm.Add("Jim"); 
  12.  
  13.     paperFirm.Add("Lucy"); 
  14.  
  15.     paperFirm.Add("Alex"); 
  16.  
  17.     paperFirm.Add("Jeff"); 
  18.  
  19.   
  20.  
  21.     paperFirm.Remove("Jim");//删除jim 
  22.  
  23.     paperFirm.Remove("Lucy");//删除lucy 
  24.  
  25.   
  26.  
  27. 元素改变后调用的方法体 
  28.  
  29. static void paperFirm_CollectionChanged(object sender,  
  30.  
  31. System.Collections.Specialized.NotifyCollectionChangedEventArgs e) 
  32.  
  33.         { 
  34.  
  35.             if (e.Action == NotifyCollectionChangedAction.Add) 
  36.  
  37.             {  
  38.  
  39.                 Console.WriteLine(string.Format("you add a element in {0},it is value is {1}",new object[]{e.NewStartingIndex,((ObservableCollection<string>)sender)[e.NewStartingIndex]})); 
  40.  
  41.             } 
  42.  
  43.             if (e.Action == NotifyCollectionChangedAction.Remove) 
  44.  
  45.             { 
  46.  
  47.                 Console.WriteLine(string.Format("you remove a element in {0},it is value is {1}", new object[] { e.OldStartingIndex,e.OldItems[0]})); 
  48.  
  49.             } 
  50.  
  51.             if (e.Action == NotifyCollectionChangedAction.Replace) 
  52.  
  53.             {  
  54.  
  55.              
  56.  
  57.             } 
  58.  
  59.             if (e.Action == NotifyCollectionChangedAction.Reset) 
  60.  
  61.             { 
  62.  
  63.                 Console.WriteLine("reset event!"); 
  64.  
  65.             } 
  66.  
  67.         } 

执行结果:

 

you add a element in 0,it is value is Jim

you add a element in 1,it is value is Lucy

you add a element in 2,it is value is Alex

you add a element in 3,it is value is Jeff

you remove a element in 0,it is value is Jim

you remove a element in 0,it is value is Lucy

小结:

    在日常的开发中,我们主要使用的数据结构并不是很多。它们主要为:值列表,数组,栈,队列或字典。在.Net4版本中又扩展了些数据结构它们分别为:有序队列,存在一对多关系的字典,可观察的集合。也添加了些接口来处理集合对象的值比较问题及集合与集合之间运算的问题。

    到目前为止.Net用来处理数据问题的接口如下图:

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:139298次
    • 积分:1887
    • 等级:
    • 排名:千里之外
    • 原创:24篇
    • 转载:65篇
    • 译文:0篇
    • 评论:26条