比如Dictionary<char,int> test;
我希望能够char和int互为key,
可以通过char快速检索到对应的int,也可以通过int快速检索到对应char
不知道大家是否明白我的意思?我要规避数组的IndexOf(效率低)
循环test检索也一样,耗时太长了
我希望能够char和int互为key,
可以通过char快速检索到对应的int,也可以通过int快速检索到对应char
不知道大家是否明白我的意思?我要规避数组的IndexOf(效率低)
循环test检索也一样,耗时太长了
我需要根据c快速检索到2,反过来,根据3能快速检索到d
var charkey = test.Where(t => t.Value == 2).First().Key;
class dictionary
{
Dictionary<char, int> test1 = new Dictionary<char, int>();
Dictionary<int, char> test2 = new Dictionary<int, char>();
public void Remove(……)
{……}
public void Add(char key, int value)
{
if (test1.ContainsKey(key) || test2.ContainsKey(value))
return;
test1.Add(key, value);
test2.Add(value, key);
}
public char? this[int index]
{
get { return test2.ContainsKey(index) ? (char?)test2[index] : null; }
}
public int? this[char index]
{
get { return test1.ContainsKey(index) ? (int?)test2[index] : null; }
}
}
class A{int pos,char chr}然后List<A>检索,快吗?
纠正一下,字典的Key不是用来检索的,而是一个索引,根据这个Key可以直接Hash计算出他存储的值在哪个位置。如果想实现你要的功能,简单,用两个字典