Vol*_*ula 5 .net c# arrays algorithm data-structures
现在我有一个动态分配数组内存的算法:
这是用于动态内存分配的相当快的算法,尽管将元素复制到新分配的数组的额外开销.
什么是更快,List<T>或基于阵列的这种算法?你会建议使用什么?
确实List<T>使用简单数组作为内部数据结构?
回答你的问题:
确实,C#的List<T>实现使用了一个内部数组
IEnumerable<T>(这意味着它可以是LINQ查询,foreach编辑等)等等
因此,我会要求您使用List<T>而不是自己的列表.
哦,顺便说一句,如果你想要微软的源代码List<T>,那么就在这里
编辑
EnsureCapacityin 的源代码List<T>是:
// Ensures that the capacity of this list is at least the given minimum
// value. If the currect capacity of the list is less than min, the
// capacity is increased to twice the current capacity or to min,
// whichever is larger.
private void EnsureCapacity(int min) {
if (_items.Length < min) {
int newCapacity = _items.Length == 0? _defaultCapacity : _items.Length * 2;
if (newCapacity < min) newCapacity = min;
Capacity = newCapacity;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3106 次 |
| 最近记录: |