C#中不常用的数据结构
锯齿数组
////从1到9打印出一个三角状的图像
int[][] table = new int[9][];
for (int i = 0; i <= table.GetUpperBound(0); i++)
{
table[i] = new int[i + 1];
for (int j = 0; j <= table[i].GetUpperBound(0); j++)
{
table[i][j] = j + 1;
}
}
for (int i = 0; i <= table.GetUpperBound(0); i++)
{
for (int j = 0; j <=table[i].GetUpperBound(0); j++)
{
Console.Write(table[i][j].ToString()+" ");
}
Console.WriteLine();
}
数组片段
//ArraySegment(数组片段)
/*
int[] data=new int[]{1,2,3,4,5,6,7,8,9};
ArraySegment<int> segment = new ArraySegment<int>(data, 4, 5);
for (int i = 0; i < segment.Count; i++)
{
Console.Write(segment.Array[segment.Offset + i].ToString()
+ " ");
}
Console.WriteLine();
*/
IStructuralEquatable和IstructuralComparable
//比较数组的等值性 ,被比较的元素需要实现接口IEuqatable
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",23)
};
if (family1 == family2)
{
Console.WriteLine("it's equal");
}
else
{
Console.Write("it's not equal");
}
if ((family1 as IStructuralEquatable).Equals(family2, EqualityComparer<Person>.Default))
{
Console.WriteLine("it's equal");
}
else
{
Console.Write("it's not equal");
}
IFormatProvider
//对象必须实现接口Iformattable
class Person:IFormattable,IComparable<Person>
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string ToString(string format, IFormatProvider formatProvider)
{
string result = string.Empty;
switch (format)
{
case "F":
result = FirstName;
break;
case "L":
result = LastName;
break;
case "FULL":
result = FirstName + LastName;
break;
default:
break;
}
return result;
}
public int CompareTo(Person other)
{
return this.FirstName.CompareTo(other.FirstName);
}
}
调用代码:
Person jim = new Person() { FirstName = "Jim", LastName = "Green", Age = 11 };
Console.WriteLine(jim.ToString("F", null));
SortedList(有序列表)
SortedList<string, string> charaterList = new SortedList<string, string>();
charaterList.Add("A", "this is charater A");
charaterList.Add("E", "this is charater E");
charaterList.Add("D", "this is charater D");
charaterList.Add("C", "this is charater C");
charaterList.Add("B", "this is charater B");
foreach(string tmp in charaterList.Keys)
{
Console.WriteLine(charaterList[tmp]);
}
执行结果:
this is charater A this is charater B this is charater C this is charater D this is charater E |
字典
Dictionary<string, string> programBook = new Dictionary<string, string>();
programBook.Add("chapter1", "base programming skill");
programBook.Add("chapter2", "the amateurism programming skill");
programBook.Add("chapter3", "the professional programming skill");
programBook.Add("chapter4", "the god programming skill");
Random rand = new Random();
string[] content = programBook.Keys.ToArray<string>();
Console.WriteLine(programBook[content[rand.Next(0, 4)]]);
Lookup(一键多值)
Person[] persons = new Person[]{
new Person{FirstName="Li",LastName="Ming",Age=21},
new Person{FirstName="Li",LastName="XinLiang",Age=22},
new Person{FirstName="Wang",LastName="Kai",Age=23},
new Person{FirstName="Li",LastName="SiMing",Age=24}
};
var personsContent = persons.ToLookup(person => person.FirstName);
ILookup<string,Person> ok = persons.ToLookup<Person, string>(a => a.FirstName);
foreach (Person tmp in personsContent["Li"])
{
Console.WriteLine(tmp.FirstName+tmp.LastName);
}
foreach (Person tmp in ok["Li"])
{
Console.WriteLine(tmp.FirstName + tmp.LastName);
}
结果:
LiMing LiXinLiang LiSiMing
LiMing LiXinLiang LiSiMing |
SortedDictionary(有序字典)
SortedDictionary<string, string> book = new SortedDictionary<string, string>();
book.Add("chapter1", "this is chapter 1");
book.Add("chapter6", "this is chapter 6");
book.Add("chapter3", "this is chapter 3");
book.Add("chapter2", "this is chapter 2");
book.Add("chapter9", "this is chapter 9");
book.Add("chapter4", "this is chapter 4");
book.Add("chapter7", "this is chapter 7");
book.Add("chapter5", "this is chapter 5");
book.Add("chapter8", "this is chapter 8");
foreach(string key in book.Keys)
{
Console.WriteLine(key + ":" + book[key]);
}
Console.WriteLine();
SortedDictionary<Person, string> contactBook = new SortedDictionary<Person, string>();
contactBook.Add(new Person { FirstName = "Li", LastName = "XiaoYao", Age = 20 }, "135****3797");
contactBook.Add(new Person { FirstName = "Zhao", LastName = "LinEr", Age = 20 }, "132****8534");
contactBook.Add(new Person { FirstName = "Lin", LastName = "YueRu", Age = 20 }, "131****6734");
contactBook.Add(new Person { FirstName = "Jiu", LastName = "Xian", Age = 20 }, "137****3215");
contactBook.Add(new Person { FirstName = "Xiao", LastName = "QiGai", Age = 20 }, "134****4751");
contactBook.Add(new Person { FirstName = "Jian", LastName = "Xian", Age = 20 }, "133****1642");
foreach (Person key in contactBook.Keys)
{
Console.WriteLine(key.FirstName+key.LastName + ":" + contactBook[key]);
}
执行结果:
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接口提供如下方法:创建合集,创建交集,或于一个集合做比较判断是否为其的子集或超集。
//无序集合HashSet
HashSet<string> charaterList = new HashSet<string>();
charaterList.Add("A");
charaterList.Add("B");
charaterList.Add("C");
if (!charaterList.Add("A"))
{
Console.WriteLine("Current Collection already have the 'A'");
}
foreach (string tmp in charaterList)
{
Console.WriteLine(tmp);
}
Console.WriteLine();
//有序集合SortedSet
SortedSet<string> charaterList2 = new SortedSet<string>();
charaterList2.Add("A");
charaterList2.Add("B");
charaterList2.Add("D");
charaterList2.Add("C");
charaterList2.Add("G");
charaterList2.Add("E");
charaterList2.Add("F");
if (!charaterList.Add("A"))
{
Console.WriteLine("Current Collection already have the 'A'");
}
foreach (string tmp in charaterList2)
{
Console.WriteLine(tmp);
}
//IsSubSetOf()和IsSuperSetOf()方法
// int[] onetoten = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
//int[] fivetoeight = new int[] { 5, 6, 7, 8 };
HashSet<int> onetoten = new HashSet<int>();
onetoten.Add(1);
onetoten.Add(2);
onetoten.Add(3);
onetoten.Add(4);
onetoten.Add(5);
onetoten.Add(6);
onetoten.Add(7);
onetoten.Add(8);
onetoten.Add(9);
onetoten.Add(10);
HashSet<int> fivetoeight = new HashSet<int>();
fivetoeight.Add(5);
fivetoeight.Add(6);
fivetoeight.Add(7);
fivetoeight.Add(8);
HashSet<int> eleventofifteen = new HashSet<int>();
eleventofifteen.Add(11);
eleventofifteen.Add(12);
eleventofifteen.Add(13);
eleventofifteen.Add(14);
eleventofifteen.Add(15);
eleventofifteen.Add(1);
if (fivetoeight.IsSubsetOf(onetoten))
{
Console.WriteLine("fivetoeight is subset of onetoten.");
}
if (fivetoeight.IsSupersetOf(fivetoeight))
{
Console.WriteLine("ontoten is superset of fivetoten");
}
//onetoten.UnionWith(eleventofifteen);//这里包含重复的元素
onetoten.ExceptWith(fivetoeight);
foreach (int tmp in onetoten)
{
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 |
可观察的集合
ObservableCollection<string> paperFirm = new ObservableCollection<string>();
string[] personList=new string[]{"Bank","Bruce"};
paperFirm.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(paperFirm_CollectionChanged);
paperFirm.Add("Jim");
paperFirm.Add("Lucy");
paperFirm.Add("Alex");
paperFirm.Add("Jeff");
paperFirm.Remove("Jim");//删除jim
paperFirm.Remove("Lucy");//删除lucy
元素改变后调用的方法体
static void paperFirm_CollectionChanged(object sender,
System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
Console.WriteLine(string.Format("you add a element in {0},it is value is {1}",new object[]{e.NewStartingIndex,((ObservableCollection<string>)sender)[e.NewStartingIndex]}));
}
if (e.Action == NotifyCollectionChangedAction.Remove)
{
Console.WriteLine(string.Format("you remove a element in {0},it is value is {1}", new object[] { e.OldStartingIndex,e.OldItems[0]}));
}
if (e.Action == NotifyCollectionChangedAction.Replace)
{
}
if (e.Action == NotifyCollectionChangedAction.Reset)
{
Console.WriteLine("reset event!");
}
}
执行结果:
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用来处理数据问题的接口如下图: