mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-01 17:53:18 +08:00
refactor: add/refactor method in include, simplified print code (#471)
This commit is contained in:
@@ -8,12 +8,12 @@ using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_hashing;
|
||||
|
||||
/* 键值对 int->String */
|
||||
/* 键值对 int->string */
|
||||
class Entry
|
||||
{
|
||||
public int key;
|
||||
public String val;
|
||||
public Entry(int key, String val)
|
||||
public string val;
|
||||
public Entry(int key, string val)
|
||||
{
|
||||
this.key = key;
|
||||
this.val = val;
|
||||
@@ -42,7 +42,7 @@ class ArrayHashMap
|
||||
}
|
||||
|
||||
/* 查询操作 */
|
||||
public String? get(int key)
|
||||
public string? get(int key)
|
||||
{
|
||||
int index = hashFunc(key);
|
||||
Entry? pair = buckets[index];
|
||||
@@ -51,7 +51,7 @@ class ArrayHashMap
|
||||
}
|
||||
|
||||
/* 添加操作 */
|
||||
public void put(int key, String val)
|
||||
public void put(int key, string val)
|
||||
{
|
||||
Entry pair = new Entry(key, val);
|
||||
int index = hashFunc(key);
|
||||
@@ -91,9 +91,9 @@ class ArrayHashMap
|
||||
}
|
||||
|
||||
/* 获取所有值 */
|
||||
public List<String> valueSet()
|
||||
public List<string> valueSet()
|
||||
{
|
||||
List<String> valueSet = new();
|
||||
List<string> valueSet = new();
|
||||
foreach (Entry? pair in buckets)
|
||||
{
|
||||
if (pair != null)
|
||||
@@ -133,7 +133,7 @@ public class array_hash_map
|
||||
|
||||
/* 查询操作 */
|
||||
// 向哈希表输入键 key ,得到值 value
|
||||
String? name = map.get(15937);
|
||||
string? name = map.get(15937);
|
||||
Console.WriteLine("\n输入学号 15937 ,查询到姓名 " + name);
|
||||
|
||||
/* 删除操作 */
|
||||
@@ -154,7 +154,7 @@ public class array_hash_map
|
||||
Console.WriteLine(key);
|
||||
}
|
||||
Console.WriteLine("\n单独遍历值 Value");
|
||||
foreach (String val in map.valueSet())
|
||||
foreach (string val in map.valueSet())
|
||||
{
|
||||
Console.WriteLine(val);
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ public class hash_map
|
||||
public void Test()
|
||||
{
|
||||
/* 初始化哈希表 */
|
||||
Dictionary<int, String> map = new();
|
||||
Dictionary<int, string> map = new();
|
||||
|
||||
/* 添加操作 */
|
||||
// 在哈希表中添加键值对 (key, value)
|
||||
@@ -26,18 +26,18 @@ public class hash_map
|
||||
map.Add(13276, "小法");
|
||||
map.Add(10583, "小鸭");
|
||||
Console.WriteLine("\n添加完成后,哈希表为\nKey -> Value");
|
||||
PrintUtil.printHashMap(map);
|
||||
PrintUtil.PrintHashMap(map);
|
||||
|
||||
/* 查询操作 */
|
||||
// 向哈希表输入键 key ,得到值 value
|
||||
String name = map[15937];
|
||||
string name = map[15937];
|
||||
Console.WriteLine("\n输入学号 15937 ,查询到姓名 " + name);
|
||||
|
||||
/* 删除操作 */
|
||||
// 在哈希表中删除键值对 (key, value)
|
||||
map.Remove(10583);
|
||||
Console.WriteLine("\n删除 10583 后,哈希表为\nKey -> Value");
|
||||
PrintUtil.printHashMap(map);
|
||||
PrintUtil.PrintHashMap(map);
|
||||
|
||||
/* 遍历哈希表 */
|
||||
Console.WriteLine("\n遍历键值对 Key->Value");
|
||||
@@ -51,7 +51,7 @@ public class hash_map
|
||||
Console.WriteLine(key);
|
||||
}
|
||||
Console.WriteLine("\n单独遍历值 Value");
|
||||
foreach (String val in map.Values)
|
||||
foreach (string val in map.Values)
|
||||
{
|
||||
Console.WriteLine(val);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user