using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class 哈希Table : MonoBehaviour {
Hashtable ht1 = new Hashtable();
// Hashtable类代表了 基于键的哈希代码组织起来的键/值对 集合
//它使用键来访问集合中的元素
//需要装箱拆箱,比起字典效率较低
void Start () {
ht1.Add("1", 100);--参数为object,需要装箱拆箱
ht1.Add(1, 99);
if(ht1.ContainsKey("1"))
{
Debug.Log("包含key为" + "1" + "的数据");
}
ht1.Remove("1");
Debug.Log(ht1["1"]);//输出null
Debug.Log(ht1["2"]);//输出null
Debug.Log(ht1[1]);//输出99
ht1[1] = 999;
ICollection key = ht1.Keys;//取到所有键
Debug.Log("遍历1");
foreach (var k in key)
{
Debug.Log(ht1[k]);//输出999
}
ht1.Clear();
Debug.Log("遍历2");
foreach (var k in key)
{
Debug.Log(ht1[k]);//无输出
}
}
}
数据结构_hashtable
最新推荐文章于 2022-03-09 01:05:59 发布