您的位置:首页 > 编程语言 > C#

C#基础课程之五集合(HashTable,Dictionary)

2017-04-08 10:37 435 查看


C#基础课程之五集合(HashTable,Dictionary)

HashTable例子:
#region HashTable
#region Add
Hashtable hashTable = new Hashtable();
Hashtable hashTableNews = new Hashtable();
string[] StringArray=new string[5];
hashTableNews.Add(1, StringArray);
hashTableNews.Add(1, 3);
hashTableNews.Add(1,4);
Console.WriteLine(hashTableNews.);
object[] Array = new object[10];
hashTable.Add(1, "第一");
hashTable.Add(2, "第二");
hashTable.Add(3, "第三");
hashTable.Add(4, "第四");
hashTable.Add(5, "第五");
hashTable.Add(6, "第六");
#endregion
#region Contains
bool ContainsResult = hashTable.Contains(2);//查找Key值
Console.WriteLine(ContainsResult);
bool ContainsKeysResult = hashTable.ContainsKey(7);//查找Key值
Console.WriteLine(ContainsKeysResult);
bool ContainsValueResult = hashTable.ContainsValue("第一");//查找Value值
Console.WriteLine(ContainsValueResult);
#endregion
int i = hashTable.Count;
Console.WriteLine(i);
foreach (DictionaryEntry dicEntry in hashTable)
{
Console.WriteLine(" " + dicEntry.Key + " " + dicEntry.Value);
}
foreach (var key in hashTable.Keys)
{
Console.WriteLine(" " + key + " " + hashTable[key]);
}
Hashtable hashTableNew = (Hashtable)hashTable.Clone();//浅表复制,只复制结构

hashTable.CopyTo(Array, 3);
foreach (var item in Array)
{
Console.WriteLine(" " + item);
}
DictionaryEntry[] Array1 = new DictionaryEntry[10];
hashTable.CopyTo(Array1, 3);
foreach (DictionaryEntry item1 in Array1)
{
Console.WriteLine(" " + item1.Key + " " + item1.Value);
}
#region Equals
Hashtable hashTable1 = new Hashtable();
hashTable1.Add(1, "第一");
hashTable1.Add(2, "第二");
hashTable1.Add(3, "第三");
hashTable1.Add(4, "第四");
hashTable1.Add(5, "第五");
hashTable1.Add(7, "第六");
bool result1 = hashTable.Equals(hashTable1);
Hashtable hashTable2 = new Hashtable();
hashTable2.Add(1, "第一");
hashTable2.Add(2, "第二");
hashTable2.Add(3, "第三");
hashTable2.Add(4, "第四");
hashTable2.Add(5, "第五");
hashTable2.Add(6, "第六");
bool result2 = hashTable.Equals(hashTable2);
Console.WriteLine(result2);
Hashtable hashTable3 = hashTable;
bool result3 = hashTable.Equals(hashTable3);
Console.WriteLine(result3);
#endregion
Console.ReadLine();
hashTable.Remove(2);
hashTable.Clear();

#endregion
#region Dictionary
Dictionary<string, int> dictionary = new Dictionary<string, int>();
Dictionary<string, string[]> dictionary1 = new Dictionary<string, string[]>();
Dictionary<string, ArrayList> dictionary2 = new Dictionary<string, ArrayList>();
Dictionary<string, emp> dictionary3 = new Dictionary<string, emp>();
Dictionary<string[], int> dictionary4 = new Dictionary<string[], int>();
string[] value = new string[5];
value[1] = "10";
dictionary1.Add("1", value);
foreach (var item in dictionary1)
{
Console.WriteLine(" " + item.Key );
foreach (var items in item.Value)
{
Console.WriteLine(" " + items);
}
}
Console.ReadLine();
#endregion
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐