我需要一个排序列表,但是在从列表中删除一个项之后,我需要在向列表中添加新项之前调整其他项的键。
您不允许更改"SortedList“中的项的键。
做这件事最好的工具是什么。
示例代码
timedEvQue.Add(3, "First");
timedEvQue.Add(7, "Second");
timedEvQue.Add(9, "Third");
int decAmnt = (int)timedEvQue.Keys[0];
timedEvQue.RemoveAt(0);
for (int i = 0; i < timedEvQue.Count; ++i)
{
timedEvQue.Keys[i] = timedEvQue.Keys[i] - decAmnt; //runtime error here
}
timedEvQue.Add(5, "Forth");发布于 2014-01-30 22:38:59
对于字典/散列映射类型的数据结构,通常不存在更改键操作,因为它们本质上只是删除并再次添加项。因此,只需删除并添加项目回来。
timedEvQue.Add(3, "First");
timedEvQue.Add(7, "Second");
timedEvQue.Add(9, "Third");
int decAmnt = (int)timedEvQue.Keys[0];
timedEvQue.RemoveAt(0);
for (int i = 0; i < timedEvQue.Count; ++i)
{
int oldKey = timedEvQue.Keys[i];
string val = timedEvQue[oldKey];
int newKey = oldKey - decAmnt;
timedEvQue.Remove(oldKey);
timedEvQue.Add(newKey, val);
}
timedEvQue.Add(5, "Forth");https://stackoverflow.com/questions/21468781
复制相似问题