【C#】 6.Set<T>数据类型
2015-02-28 23:12
274 查看
using System; using System.Collections.Generic; using System.Collections; using System.Linq; using System.Text; namespace UserDefinedDataEXP { public class Set<T> : IEnumerable<T> { // .. Dictionary<T, int> dict; // etc. //构造器1 public Set() { dict = new Dictionary<T, int>(); } //构造器2 public Set(Set<T> set) { dict = new Dictionary<T, int>(); foreach (T key in set.dict.Keys) { dict[key]=0; } } //GetEnumerator迭代器 IEnumerator<T> IEnumerable<T>.GetEnumerator() { foreach (KeyValuePair<T, int> item in dict) { yield return item.Key; } } IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); } //Insert方法 public void Insert(T key) { dict[key]=0; } //Remove方法 public void Remove(T key) { dict.Remove(key); } //Contains方法 public bool Contains(T key) { if (dict.ContainsKey(key)) { return true; } else return false; } //Replace方法 public void Replace(T key1,T key2) { foreach (T key in dict.Keys) { if (key.Equals(key1)) { Remove(key1); Insert(key2); break; } } } //Intersection方法 public static Set<T> Intersection(Set<T> s1, Set<T> s2) { Set<T> setIntsec = new Set<T>(); foreach (T key1 in s1.dict.Keys) { foreach (T key2 in s2.dict.Keys) { if (key1.Equals(key2)) setIntsec.Insert(key1); } } return setIntsec; } //Union方法 public static Set<T> Union(Set<T> s1, Set<T> s2) { Set<T> setUnion = new Set<T>(s1); foreach (T key2 in s2.dict.Keys) { setUnion.Insert(key2); } return setUnion; } //Difference方法 public static Set<T> Difference(Set<T> s1, Set<T> s2) { Set<T> setDiff = new Set<T>(s1); foreach (T key1 in s1.dict.Keys) { foreach (T key2 in s2.dict.Keys) { if (key1.Equals(key2)) setDiff.Remove(key1); } } return setDiff; } //SymmetricDifference方法 public static Set<T> SymmetricDifference(Set<T> s1, Set<T> s2) { Set<T> setSymDiff = Set<T>.Union(s1,s2); foreach (T key1 in s1.dict.Keys) { foreach (T key2 in s2.dict.Keys) { if (key1.Equals(key2)) setSymDiff.Remove(key1); } } return setSymDiff; } //print方法 public void print() { foreach (KeyValuePair<T,int> item in dict) { Console.Write("{0}, ", item.Key); } Console.WriteLine(); } //count属性 public int count { get { return dict.Count; } } } <pre name="code" class="csharp">
class test
{
static void Main(string[] args)
{
Set<string> names = new Set<string>();
names.Insert("A1");
names.Insert("A2");
names.Insert("A3");
names.Insert("A4");
names.Insert("B1");
names.print();
Set<string> namesII = new Set<string>();
namesII.Insert("A1");
namesII.Insert("X2");
namesII.Insert("X2");
namesII.Insert("BB");
namesII.Insert("B1");
// Surgery
namesII.Remove("X2");
if (namesII.Contains("X2"))
{
Console.WriteLine("ugh, X2 is still there");
}
else
{
Console.WriteLine("X2 has been removed");
}
namesII.Replace("B1", "b1");
namesII.print();
// 'Interactions' between sets (static methods)
Set<string> result = Set<string>.Intersection(names, namesII);
result.print();
result = Set<string>.Union(names, namesII);
result.print();
result = Set<string>.Difference(names, namesII);
result.print();
result = Set<string>.SymmetricDifference(names, namesII);
result.print();
Console.ReadLine();
}}}
相关文章推荐
- &lt;展现C#&gt;第四章C#类型
- Java基础<二>--->关键字、数据类型、运算
- mybatis查询如何返回List<Map>类型数据
- Redis Hash类型数据常用命令总结<转>
- C#读取数据库返回泛型集合 把DataSet类型转换为List<T>泛型集合
- KeyValuePair<byte, string> 类型的数据如何绑定到ComboBox控件上
- <Head First Java>学习笔记--第三章:primitive主数据类型和引用
- Swift 基本数据类型<整型/浮点型>
- ArrayList、List<T>、HashSet<T>、LinkedList<T>各自优点和缺点,Dictionary<K,V>的内部存储数据方式有什么特殊的?
- C#_delegate - Pair<T> & 简单顺序逆序 & 方法委托(在Pair类下)&枚举类型 混搭使用
- System.Nullable<T> int? 数据库中的int类型可以为空 在C# 中 的处理
- 黑马程序员 Java基础<二>--->数据类型,数组,switch语句
- c# 泛型<T>类型参数T的约束where
- c# Json List<T> DataTable 数据相互转换
- c# Json List<T> DataTable 数据相互转换
- 如何在ArrayList<Integer>中添加String类型数据
- C#使用集合组织数据(HashTable、ArrayList、List<T>,Dictionary<K,V>
- <<展现C#>>第四章 C#类型(修订)
- c# Json List<T> DataTable 数据相互转换
- Rust之语法<3>数据类型