c# - 快速插入类链接对象

标签 c# object

在基于代理的实时建模项目中,我遇到以下问题:假设我有一些带有公共(public)属性 X(X 是 double )的 MyClass 类。我需要一个可以按排序方式包含我的对象的类(称之为 FancyList)。如果我插入一个新的 MyClass 对象,它应该插入到具有较低 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 .根据the MSDN docs 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);

关于c# - 快速插入类链接对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24587035/

相关文章:

c# - 为什么我不能在封闭泛型的继承属性中使用 C# 语法,但 CLR 语法很好

c# - 将枚举绑定(bind)到 WinForms 组合框,然后设置它

c# - 如何在 WPF 中创建 TableLayoutPanel?

Wordpress - 将多个 WP 查询对象合而为一?

java - 如何从android中的Map中获取键值对

c# - 创建自定义全尺寸日历控件

c# - 无论如何在 C# 中反序列化之前检查对象的类类型?

javascript - JS中的字符串到对象

循环中的 C++ 对象引用

javascript - 计算对象内部对象内部的值