Chinaunix首页 | 论坛 | 博客

idn

  • 博客访问: 45358
  • 博文数量: 22
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 180
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-14 21:30
文章分类

全部博文(22)

文章存档

2010年(9)

2009年(10)

2008年(3)

我的朋友
最近访客

分类:

2009-11-27 10:17:47

专用集合是预定义集合,用于特殊或专用性很强的场合,这些集合位于System.Collections.Specialized命名空间并且被看作是泛型集合的扩展.要想理解.netFramework为什么提供这些集合,可考虑下列实例并思考将如何做到以下几点:

◆想要使用与 Hashtable 类相似的键/值对来实现一个小型集合以存放最多 10 个元素。
◆想要实现类型安全的 ArrayList来存放字符串值,并为其提供适当的枚举数。
◆要根据键/值对或使用索引位置来访问集合的元素。
◆要创建一个不区分大小写的 SortedList 类。

我们可以通过使用某些具有特定用途的预定义集合来达到上述所有目的。这些预定义的特殊集合称为专用集合,专用集合有以下几种:

◆StringCollection类

◆StringDictionary类

◆StringEnumerator类

◆CollectionUtil类

◆ListDictionary类

◆HybridDictionary类

◆OrderedDictionary类

◆NamueCollection类

//StringCollection类
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Collections.Specialized;
namespace zyh{
public classContractorTest
{
public static void Main()
{
StringCollection sc = new StringCollection();
sc.Add("first");
sc.Add("Second");
sc.Add("third");
sc.Add("Third");
for (int x = 0; x < sc.Count; x++)
{
Console.WriteLine(sc[x]);
}
Console.ReadLine();
}
}
}

//StringDictionary类

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Collections.Specialized;
namespace zyh

{
public classContractorTest
{
public static void Main()
{
StringDictionary conInfo = new StringDictionary();
conInfo.Add("host", "Server1");
conInfo.Add("port", "1234");
conInfo.Add("userid", "admin");
conInfo.Add("password", "root123");
foreach (DictionaryEntry de in conInfo)
{
Console.WriteLine("{0} = {1}", de.Key, de.Value);
}
Console.ReadLine();
}
}
//StringEnumerator类

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Collections.Specialized;
namespace zyh
{
public classContractorTest
{
public static void Main()
{
StringCollection sc = new StringCollection();
sc.Add("first");
sc.Add("second");
sc.Add("third");

StringEnumerator se = sc.GetEnumerator();
while (se.MoveNext())
{
Console.WriteLine(se.Current);
}
Console.ReadLine();
}
}
}
//CollectionUtil类


using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Collections.Specialized;
namespace zyh

{
public classContractorTest
{
public static void Main()
{
Hashtable cart =CollectionsUtil.CreateCaseInsensitiveHas htable();
cart.Add("ID-1", "Item 1");
cart.Add("id-3", "Item 3");
cart.Add("Id-4", "Item 4");
if (cart.ContainsKey("id-3"))
cart.Remove("ID-3");
foreach (DictionaryEntry de in cart)
Console.WriteLine("{0} : {1}", de.Key, de.Value);
Console.ReadLine();
}
}

}

//ListDictionary类

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Collections.Specialized;
namespace zyh

{
public classContractorTest
{
public static void Main()
{
ListDictionary req = new ListDictionary();
req.Add("OS", "Windows 2000 or higher");
req.Add("RAM", "512 MB or higher");
req.Add("HDSpace", "2GB or higher");
req.Add(".NET Framework", "version 2.0");
req.Add("IIS", "6.0 or higher");

string[] keys = new string[req.Count];
req.Keys.CopyTo(keys, 0);

for (int x = 0; x < keys.Length; x++)
{
Console.WriteLine("{0} = {1}", keys[x], req[keys[x]]);
}

Console.WriteLine("Required Hard Disk Space: {0}",req["HDSpace"]);
Console.ReadLine();
}
}

}

//HybridDictionary类
usingSystem;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Collections.Specialized;
namespace zyh

{
public classContractorTest
{
public static void Main()
{
HybridDictionary styles = new HybridDictionary();
styles.Add("fore-color", "black");
styles.Add("back-color", "white");
styles.Add("font-family", "Arial");
styles.Add("font-size", "9pt");
styles.Add("font-weight", "normal");
styles.Add("line-height", "1");
styles.Add("border-width", "0");
styles.Add("margin", "2");
styles.Add("text-decoration", "none");
styles.Add("padding", "0px");
styles.Add("border-color", "none");
styles.Add("width", "100px");
styles.Add("height", "22px");

foreach (DictionaryEntry de in styles)
Console.WriteLine("{0} = {1}", de.Key, de.Value);

Console.ReadLine();

}
}

}

//OrderedDictionary类


using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Collections.Specialized;
namespace zyh

{
public classContractorTest
{
public static void Main()
{
OrderedDictionary od = new OrderedDictionary();
od.Add("1", "One");
od.Add("2", "Two");
od.Add("4", "Four");
od.Add("5", "Five");
od.Insert(2, "3", "Three");
od.Insert(od.Count, "6", "Six");

foreach (DictionaryEntry de in od)
Console.WriteLine("{0} = {1}", de.Key, de.Value);
Console.ReadLine();
}
}

}

//NamueCollection类

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Collections.Specialized;
namespace zyh{
public classContractorTest
{
public static void Main()
{
NamueCollection nvc = new NamueCollection();
nvc.Add("city", "New York");
nvc.Add("country", "U.S.");
nvc.Add("city", "California");
nvc.Remove("country");
nvc.Add("country", "United States");

foreach (string key in nvc.AllKeys)
Console.WriteLine("{0} = {1}", key, nvc.Get(key));

// alternate way
for (int x = 0; x < nvc.Count; x++)
{
Console.WriteLine(nvc.Get(x));
}

Console.ReadLine();
}
}

}

//使用专用位结构在内存中高效地存储数据


using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Collections.Specialized;
namespace zyh
{
public classSamplesBitVector32
{
public static void Main()
{
// Creates and initializes a BitVector32.
BitVector32 myBV = new BitVector32(0);

// Creates four sections in the BitVector32 with maximum values 6,3, 1, and 15.
// mySect3, which uses exactly one bit, can also be used as a bitflag.
BitVector32.Section mySect1 = BitVector32.CreateSection(6);
BitVector32.Section mySect2 = BitVector32.CreateSection(3,mySect1);
BitVector32.Section mySect3 = BitVector32.CreateSection(1,mySect2);
BitVector32.Section mySect4 = BitVector32.CreateSection(15,mySect3);

// Displays the values of the sections.
Console.WriteLine("Initial values:");
Console.WriteLine("\tmySect1: {0}", myBV[mySect1]);
Console.WriteLine("\tmySect2: {0}", myBV[mySect2]);
Console.WriteLine("\tmySect3: {0}", myBV[mySect3]);
Console.WriteLine("\tmySect4: {0}", myBV[mySect4]);

// Sets each section to a new value and displays the value of theBitVector32 at each step.
Console.WriteLine("Changing the values of each section:");
Console.WriteLine("\tInitial:  \t{0}", myBV.ToString());
myBV[mySect1] = 5;
Console.WriteLine("\tmySect1 = 5:\t{0}", myBV.ToString());
myBV[mySect2] = 3;
Console.WriteLine("\tmySect2 = 3:\t{0}", myBV.ToString());
myBV[mySect3] = 1;
Console.WriteLine("\tmySect3 = 1:\t{0}", myBV.ToString());
myBV[mySect4] = 9;
Console.WriteLine("\tmySect4 = 9:\t{0}", myBV.ToString());

// Displays the values of the sections.
Console.WriteLine("New values:");
Console.WriteLine("\tmySect1: {0}", myBV[mySect1]);
Console.WriteLine("\tmySect2: {0}", myBV[mySect2]);
Console.WriteLine("\tmySect3: {0}", myBV[mySect3]);
Console.WriteLine("\tmySect4: {0}", myBV[mySect4]);
Console.ReadLine();
}
}


}

阅读(369) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~