// Removes the element at the given index. The size of the list is
// decreased by one.
//
public void RemoveAt(int index) {
if ((uint)index >= (uint)_size) {
ThrowHelper.ThrowArgumentOutOfRangeException();
}
Contract.EndContractBlock();
_size--;
if (index < _size) {
Array.Copy(_items, index + , _items, index, _size - index);
}
_items[_size] = default(T);
_version++;
}

可以看到 Remove的操作里涉及对List的存储空间重新赋值的工作.而多余占用的空间用Default(T)来填充并未回收

// Inserts the elements of the given collection at a given index. If
// required, the capacity of the list is increased to twice the previous
// capacity or the new size, whichever is larger. Ranges may be added
// to the end of the list by setting index to the List's size.
//
public void InsertRange(int index, IEnumerable<T> collection) {
if (collection==null) {
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
} if ((uint)index > (uint)_size) {
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_Index);
}
Contract.EndContractBlock(); ICollection<T> c = collection as ICollection<T>;
if( c != null ) { // if collection is ICollection<T>
int count = c.Count;
if (count > ) {
EnsureCapacity(_size + count);
if (index < _size) {
Array.Copy(_items, index, _items, index + count, _size - index);
} // If we're inserting a List into itself, we want to be able to deal with that.
if (this == c) {
// Copy first part of _items to insert location
Array.Copy(_items, , _items, index, index);
// Copy last part of _items back to inserted location
Array.Copy(_items, index+count, _items, index*, _size-index);
}
else {
T[] itemsToInsert = new T[count];
c.CopyTo(itemsToInsert, );
itemsToInsert.CopyTo(_items, index);
}
_size += count;
}
}
else {
using(IEnumerator<T> en = collection.GetEnumerator()) {
while(en.MoveNext()) {
Insert(index++, en.Current);
}
}
}
_version++;
}
 // 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 == ? _defaultCapacity : _items.Length * ;
// Allow the list to grow to maximum possible capacity (~2G elements) before encountering overflow.
// Note that this check works even when _items.Length overflowed thanks to the (uint) cast
if ((uint)newCapacity > Array.MaxArrayLength) newCapacity = Array.MaxArrayLength;
if (newCapacity < min) newCapacity = min;
Capacity = newCapacity;
}
}
// Gets and sets the capacity of this list.  The capacity is the size of
// the internal array used to hold items. When set, the internal
// array of the list is reallocated to the given capacity.
//
public int Capacity {
get {
Contract.Ensures(Contract.Result<int>() >= );
return _items.Length;
}
set {
if (value < _size) {
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value, ExceptionResource.ArgumentOutOfRange_SmallCapacity);
}
Contract.EndContractBlock(); if (value != _items.Length) {
if (value > ) {
T[] newItems = new T[value];
if (_size > ) {
Array.Copy(_items, , newItems, , _size);
}
_items = newItems;
}
else {
_items = _emptyArray;
}
}
}
}

2.1 在尾部插入速度最快

2.2 在中间插入,涉及数组拷贝

2.3 元素不够时,涉及重新分配内在和数组拷贝

最新文章

  1. Oracle约束(Constraint)详解
  2. 【zz】matlab 均值方差
  3. [Doxygen]Doxygen
  4. windows 下 gvim/vim lua支持问题,neocomplete等插件支持
  5. python 多线程就这么简单
  6. boost.compressed_pair源码剖析
  7. Nodejs_day04
  8. 获取html上元素的真正坐标
  9. 包管理器Bower使用手册之一
  10. Rouh set 入门知识3(上下近似集,正负域,边界域)
  11. CSS块级元素与行级元素(转载)
  12. 使用mitmproxy嗅探双向认证ssl链接——嗅探AWS IoT SDK的mqtts
  13. IIC为什么要配置为开漏输出呢?
  14. Paper Reading: Stereo DSO
  15. Postgres通用翻页函数
  16. hdu3183 rmq求区间最值的下标
  17. 根据时间段获取时间段内所有时间点(js)
  18. crontab -e文件存放路径
  19. V-rep学习笔记:main script and child scripts
  20. centos7 --ngnix 常用命令:

热门文章

  1. The &#39;Microsoft Jet OLEDB 4.0 Provider&#39; is not registered on the local machine
  2. 数据标准化方法及其Python代码实现
  3. Yii2邮箱发送与配置
  4. CLR 显示实现事件 EventSet内部管理一个字典
  5. MFC可视化
  6. (转)15个非常棒的jQuery无限滚动插件【瀑布流效果】
  7. postfix 安装配置详解
  8. 装饰(Decorator)模式
  9. 小程序上传多图片多附件多视频 c#后端
  10. SQL server 查询语句优先级-摘抄