From 6cc1b0d4f96963cda6d7eeb8c3bedaad94592aad Mon Sep 17 00:00:00 2001 From: Kister Hakusan <2753888203@qq.com> Date: Sat, 11 Oct 2025 19:30:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Lesson4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- C#进阶/C#进阶.sln | 12 ++ .../Lesson4_Hashtable/Lesson4_Hashtable.csproj | 10 ++ C#进阶/Lesson4_Hashtable/Program.cs | 109 ++++++++++++++++++ .../Lesson4_Hashtable练习题.csproj | 10 ++ C#进阶/Lesson4_Hashtable练习题/Program.cs | 70 +++++++++++ 5 files changed, 211 insertions(+) create mode 100644 C#进阶/Lesson4_Hashtable/Lesson4_Hashtable.csproj create mode 100644 C#进阶/Lesson4_Hashtable/Program.cs create mode 100644 C#进阶/Lesson4_Hashtable练习题/Lesson4_Hashtable练习题.csproj create mode 100644 C#进阶/Lesson4_Hashtable练习题/Program.cs diff --git a/C#进阶/C#进阶.sln b/C#进阶/C#进阶.sln index 740d55f..057b2ec 100644 --- a/C#进阶/C#进阶.sln +++ b/C#进阶/C#进阶.sln @@ -17,6 +17,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson3_Queue", "Lesson3_Qu EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson3_Queue练习题", "Lesson3_Queue练习题\Lesson3_Queue练习题.csproj", "{77AE0EA5-FD54-46A0-A708-A2B38ABEE26D}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson4_Hashtable", "Lesson4_Hashtable\Lesson4_Hashtable.csproj", "{C703B2F8-529B-46E0-8D8E-07750A812966}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson4_Hashtable练习题", "Lesson4_Hashtable练习题\Lesson4_Hashtable练习题.csproj", "{962C9536-7CD7-4F5A-9D1E-1DCF986953D4}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -51,6 +55,14 @@ Global {77AE0EA5-FD54-46A0-A708-A2B38ABEE26D}.Debug|Any CPU.Build.0 = Debug|Any CPU {77AE0EA5-FD54-46A0-A708-A2B38ABEE26D}.Release|Any CPU.ActiveCfg = Release|Any CPU {77AE0EA5-FD54-46A0-A708-A2B38ABEE26D}.Release|Any CPU.Build.0 = Release|Any CPU + {C703B2F8-529B-46E0-8D8E-07750A812966}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C703B2F8-529B-46E0-8D8E-07750A812966}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C703B2F8-529B-46E0-8D8E-07750A812966}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C703B2F8-529B-46E0-8D8E-07750A812966}.Release|Any CPU.Build.0 = Release|Any CPU + {962C9536-7CD7-4F5A-9D1E-1DCF986953D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {962C9536-7CD7-4F5A-9D1E-1DCF986953D4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {962C9536-7CD7-4F5A-9D1E-1DCF986953D4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {962C9536-7CD7-4F5A-9D1E-1DCF986953D4}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/C#进阶/Lesson4_Hashtable/Lesson4_Hashtable.csproj b/C#进阶/Lesson4_Hashtable/Lesson4_Hashtable.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/C#进阶/Lesson4_Hashtable/Lesson4_Hashtable.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/C#进阶/Lesson4_Hashtable/Program.cs b/C#进阶/Lesson4_Hashtable/Program.cs new file mode 100644 index 0000000..f762f54 --- /dev/null +++ b/C#进阶/Lesson4_Hashtable/Program.cs @@ -0,0 +1,109 @@ +using System.Collections; + +namespace Lesson4_Hashtable +{ + internal class Program + { + static void Main(string[] args) + { + #region 知识点一 Hashtable的本质 + //Hashtable(又称散列表)是基于键的哈希代码组织起来的键/值对的集合。 + //它的主要作用是提高数据查询的效率 + //使用键来快速查找对应的值,而不需要遍历整个集合。 + #endregion + + #region 知识点二 声明 + //需要引用system.collections命名空间 + Hashtable hashtable = new Hashtable(); + #endregion + + #region 知识点三 增删查改 + //只能通过键来操作值 + + //增 + hashtable.Add("001", "张三"); + // ↑键 ↑值 + hashtable.Add("002", "李四"); + hashtable.Add(true, false); + hashtable.Add(false, false); + //注意:不能出现相同的键 + + + //删 + //1.只能通过键来删除 + hashtable.Remove("001"); + //2.删除不存在的键不会报错也不会有任何反应 + hashtable.Remove("003"); + //3.清空 + hashtable.Clear(); + hashtable.Add(1, "张三"); + hashtable.Add(2, "李四"); + hashtable.Add(3, false); + hashtable.Add(true, false); + + + //查 + //1.通过键来查找值,找不到返回null + Console.WriteLine(hashtable[1]);//找键值为1的值 + Console.WriteLine(hashtable[4]);//不存在,返回null + Console.WriteLine(hashtable[true]);//找键值为true的值 + //2.查看是否存在 + //根据键检测 + if(hashtable.ContainsKey(2)) + { + Console.WriteLine("存在键为2的元素"); + } + if(hashtable.ContainsKey(4)) + { + Console.WriteLine("存在键为4的元素"); + } + //根据值检测 + if(hashtable.ContainsValue("李四")) + { + Console.WriteLine("存在值为李四的元素"); + } + + + //改 + hashtable[1] = "王五";//修改键为1的值 + #endregion + + #region 知识点四 遍历 + //1.对数 + Console.WriteLine(hashtable.Count); + + //2.遍历所有键 + foreach (object item in hashtable.Keys) + { + Console.WriteLine("键"+item); + Console.WriteLine("值"+hashtable[item]); + } + + //3.遍历所有值 + foreach (object item in hashtable.Values) + { + Console.WriteLine("值"+item); + } + + //3.键值对一起遍历 + foreach (DictionaryEntry item in hashtable) + { + Console.WriteLine("键值"+item.Key+item.Value); + } + + //4.迭代器遍历法 + IDictionaryEnumerator myEnumerator = hashtable.GetEnumerator(); + bool flag = myEnumerator.MoveNext(); + while (flag) + { + Console.WriteLine("键值" + myEnumerator.Key + myEnumerator.Value); + flag = myEnumerator.MoveNext(); + } + #endregion + + #region 知识点五 装箱拆箱 + //一样的 + #endregion + } + } +} diff --git a/C#进阶/Lesson4_Hashtable练习题/Lesson4_Hashtable练习题.csproj b/C#进阶/Lesson4_Hashtable练习题/Lesson4_Hashtable练习题.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/C#进阶/Lesson4_Hashtable练习题/Lesson4_Hashtable练习题.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/C#进阶/Lesson4_Hashtable练习题/Program.cs b/C#进阶/Lesson4_Hashtable练习题/Program.cs new file mode 100644 index 0000000..11d309d --- /dev/null +++ b/C#进阶/Lesson4_Hashtable练习题/Program.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections; +using System.Security.Cryptography.X509Certificates; +using System.Threading; +namespace Lesson4_Hashtable练习题 +{ + //因为一般管理器都是唯一的,所以做成单例模式的对象 + class MonsterMgr + { + private static MonsterMgr instance = new MonsterMgr(); + private Hashtable monstersTable = new Hashtable(); + private MonsterMgr() + { + + } + public static MonsterMgr Instance + { + get + { + return instance; + } + } + + private int monsterID = 0; + + public void AddMonster() + { + + Monster monster = new Monster(monsterID); + Console.WriteLine("创建了怪物"+monsterID); + monsterID++; + monstersTable.Add(monster.id, monster); + } + + public void DeleteMonster(int monsterID) + { + if (monstersTable.ContainsKey(monsterID)) + { + (monstersTable[monsterID] as Monster).Dead(); + monstersTable.Remove(monsterID); + } + } + } + class Monster + { + public int id; + public Monster(int id) + { + this.id = id; + } + public void Dead() + { + Console.WriteLine("怪物"+id+"死亡"); + } + } + class Program + { + static void Main(string[] args) + { + MonsterMgr.Instance.AddMonster(); + MonsterMgr.Instance.AddMonster(); + MonsterMgr.Instance.AddMonster(); + MonsterMgr.Instance.AddMonster(); + MonsterMgr.Instance.AddMonster(); + MonsterMgr.Instance.DeleteMonster(0); + MonsterMgr.Instance.DeleteMonster(3); + + } + } +}