快速插入类链接对象

本文关键字:链接 对象 插入 | 更新日期: 2023-09-27 18:11:29

在一个实时的基于agent的建模项目中,我有以下问题:假设我有一些类MyClass,其公共比例为X (X是double)。我需要一个类(称为FancyList),它可以以排序的方式包含我的对象。如果我插入一个新的MyClass-object,它应该插入到一个X值较低的对象和一个X值较高的对象之间。

FancyList _fancyList = new FancyList();
// Allocate MyClass objects. The X proporty is defined in the constructor
MyClass c1 = new MyClass(2.0)
MyClass c2 = new MyClass(3.0)
MyClass c3 = new MyClass(2.5)
_fancyList.Insert(c1);
_fancyList.Insert(c2);
_fancyList.Insert(c3);
// In the fancyList c3 is after c1 and before c2. Therefore c1 is the first
// element, and c2 is the last element

快速插入类链接对象

使用SortedList<>SortedDictionary<>

它们都实现了IDictionary。根据MSDN文档,SortedDictionary具有更快的插入操作。一定要把剩下的说明书看完。

FancyList _fancyList = new SortedDictionar<double, MyClass>();
// Allocate MyClass objects. The X proporty is defined in the constructor
...
_fancyList.Insert(c1.X, c1);
_fancyList.Insert(c2.X, c2);
_fancyList.Insert(c3.X, c3);