命名空间
using System.Collections;
创建一个集合对象
ArrayList list = new ArrayList();
里面的方法我实在不想一个一个测试,直接看源代码吧
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Threading;
namespace System.Collections
{
// 无参构造函数,internal表示只能在同一类中访问
internal ArrayList(bool trash)
{
}
// 无参构造函数
public ArrayList()
{
_items = emptyArray;
}
// 单参数构造函数,参数是一个整数
public ArrayList(int capacity)
{
if (capacity < 0)
{
// 抛出异常
throw new ArgumentOutOfRangeException("capacity", Environment.GetResourceString("ArgumentOutOfRange_MustBeNonNegNum", "capacity"));
}
if (capacity == 0)
{
_items = emptyArray;
}
else
{
_items = new object[capacity];
}
}
// 单参数构造函数,参数是一个ICollection类型
public ArrayList(ICollection c)
{
if (c == null)
{
throw new ArgumentNullException("c", Environment.GetResourceString("ArgumentNull_Collection"));
}
int count = c.Count;
if (count == 0)
{
_items = emptyArray;
return;
}
_items = new object[count];
AddRange(c);
}
// 单参数构造函数,参数是一个IList类型
public static ArrayList Adapter(IList list)
{
if (list == null)
{
throw new ArgumentNullException("list");
}
return new IListWrapper(list);
}
// 在集合末尾添加一个元素value
public virtual int Add(object value)
{
if (_size == _items.Length)
{
EnsureCapacity(_size + 1);
}
_items[_size] = value;
_version++;
return _size++;
}
// InsertRange方法
public virtual void AddRange(ICollection c)
{
InsertRange(_size, c);
}
// 二分查找法,查找元素
public virtual int BinarySearch(int index, int count, object value, IComparer comparer)
{
if (index < 0)
{
throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
}
if (count < 0)
{
throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
}
if (_size - index < count)
{
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
}
return Array.BinarySearch(_items, index, count, value, comparer);
}
public virtual int BinarySearch(object value)
{
return BinarySearch(0, Count, value, null);
}
public virtual int BinarySearch(object value, IComparer comparer)
{
return BinarySearch(0, Count, value, comparer);
}
// 清除集合元素
public virtual void Clear()
{
if (_size > 0)
{
Array.Clear(_items, 0, _size);
_size = 0;
}
_version++;
}
// 克隆集合
public virtual object Clone()
{
ArrayList arrayList = new ArrayList(_size);
arrayList._size = _size;
arrayList._version = _version;
Array.Copy(_items, 0, arrayList._items, 0, _size);
return arrayList;
}
// 查看元素是否存在
public virtual bool Contains(object item)
{
if (item == null)
{
for (int i = 0; i < _size; i++)
{
if (_items[i] == null)
{
return true;
}
}
return false;
}
for (int j = 0; j < _size; j++)
{
if (_items[j] != null && _items[j].Equals(item))
{
return true;
}
}
return false;
}
// 复制元素到某个array
public virtual void CopyTo(Array array)
{
CopyTo(array, 0);
}
public virtual void CopyTo(Array array, int arrayIndex)
{
if (array != null && array.Rank != 1)
{
throw new ArgumentException(Environment.GetResourceString("Arg_RankMultiDimNotSupported"));
}
Array.Copy(_items, 0, array, arrayIndex, _size);
}
public virtual void CopyTo(int index, Array array, int arrayIndex, int count)
{
if (_size - index < count)
{
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
}
if (array != null && array.Rank != 1)
{
throw new ArgumentException(Environment.GetResourceString("Arg_RankMultiDimNotSupported"));
}
Array.Copy(_items, index, array, arrayIndex, count);
}
// 改变容量
private void EnsureCapacity(int min)
{
if (_items.Length < min)
{
int num = (_items.Length == 0) ? 4 : (_items.Length * 2);
if ((uint)num > 2146435071u)
{
num = 2146435071;
}
if (num < min)
{
num = min;
}
Capacity = num;
}
}
public static IList FixedSize(IList list)
{
if (list == null)
{
throw new ArgumentNullException("list");
}
return new FixedSizeList(list);
}
public static ArrayList FixedSize(ArrayList list)
{
if (list == null)
{
throw new ArgumentNullException("list");
}
return new FixedSizeArrayList(list);
}
public virtual IEnumerator GetEnumerator()
{
return new ArrayListEnumeratorSimple(this);
}
public virtual IEnumerator GetEnumerator(int index, int count)
{
if (index < 0)
{
throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
}
if (count < 0)
{
throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
}
if (_size - index < count)
{
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
}
return new ArrayListEnumerator(this, index, count);
}
// 插入元素
public virtual int IndexOf(object value)
{
return Array.IndexOf((Array)_items, value, 0, _size);
}
// 某个位置插入元素
public virtual int IndexOf(object value, int startIndex)
{
if (startIndex > _size)
{
throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
}
return Array.IndexOf((Array)_items, value, startIndex, _size - startIndex);
}
public virtual int IndexOf(object value, int startIndex, int count)
{
if (startIndex > _size)
{
throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
}
if (count < 0 || startIndex > _size - count)
{
throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_Count"));
}
return Array.IndexOf((Array)_items, value, startIndex, count);
}
public virtual void Insert(int index, object value)
{
if (index < 0 || index > _size)
{
throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_ArrayListInsert"));
}
if (_size == _items.Length)
{
EnsureCapacity(_size + 1);
}
if (index < _size)
{
Array.Copy(_items, index, _items, index + 1, _size - index);
}
_items[index] = value;
_size++;
_version++;
}
public virtual void InsertRange(int index, ICollection c)
{
if (c == null)
{
throw new ArgumentNullException("c", Environment.GetResourceString("ArgumentNull_Collection"));
}
if (index < 0 || index > _size)
{
throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
}
int count = c.Count;
if (count > 0)
{
EnsureCapacity(_size + count);
if (index < _size)
{
Array.Copy(_items, index, _items, index + count, _size - index);
}
object[] array = new object[count];
c.CopyTo(array, 0);
array.CopyTo(_items, index);
_size += count;
_version++;
}
}
public virtual int LastIndexOf(object value)
{
return LastIndexOf(value, _size - 1, _size);
}
public virtual int LastIndexOf(object value, int startIndex)
{
if (startIndex >= _size)
{
throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
}
return LastIndexOf(value, startIndex, startIndex + 1);
}
public virtual int LastIndexOf(object value, int startIndex, int count)
{
if (Count != 0 && (startIndex < 0 || count < 0))
{
throw new ArgumentOutOfRangeException((startIndex < 0) ? "startIndex" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
}
if (_size == 0)
{
return -1;
}
if (startIndex >= _size || count > startIndex + 1)
{
throw new ArgumentOutOfRangeException((startIndex >= _size) ? "startIndex" : "count", Environment.GetResourceString("ArgumentOutOfRange_BiggerThanCollection"));
}
return Array.LastIndexOf((Array)_items, value, startIndex, count);
}
public static IList ReadOnly(IList list)
{
if (list == null)
{
throw new ArgumentNullException("list");
}
return new ReadOnlyList(list);
}
public static ArrayList ReadOnly(ArrayList list)
{
if (list == null)
{
throw new ArgumentNullException("list");
}
return new ReadOnlyArrayList(list);
}
public virtual void Remove(object obj)
{
int num = IndexOf(obj);
if (num >= 0)
{
RemoveAt(num);
}
}
public virtual void RemoveAt(int index)
{
if (index < 0 || index >= _size)
{
throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
}
_size--;
if (index < _size)
{
Array.Copy(_items, index + 1, _items, index, _size - index);
}
_items[_size] = null;
_version++;
}
public virtual void RemoveRange(int index, int count)
{
if (index < 0)
{
throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
}
if (count < 0)
{
throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
}
if (_size - index < count)
{
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
}
if (count > 0)
{
int num = _size;
_size -= count;
if (index < _size)
{
Array.Copy(_items, index + count, _items, index, _size - index);
}
while (num > _size)
{
_items[--num] = null;
}
_version++;
}
}
public static ArrayList Repeat(object value, int count)
{
if (count < 0)
{
throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
}
ArrayList arrayList = new ArrayList((count > 4) ? count : 4);
for (int i = 0; i < count; i++)
{
arrayList.Add(value);
}
return arrayList;
}
public virtual void Reverse()
{
Reverse(0, Count);
}
public virtual void Reverse(int index, int count)
{
if (index < 0)
{
throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
}
if (count < 0)
{
throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
}
if (_size - index < count)
{
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
}
Array.Reverse(_items, index, count);
_version++;
}
public virtual void SetRange(int index, ICollection c)
{
if (c == null)
{
throw new ArgumentNullException("c", Environment.GetResourceString("ArgumentNull_Collection"));
}
int count = c.Count;
if (index < 0 || index > _size - count)
{
throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
}
if (count > 0)
{
c.CopyTo(_items, index);
_version++;
}
}
public virtual ArrayList GetRange(int index, int count)
{
if (index < 0 || count < 0)
{
throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
}
if (_size - index < count)
{
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
}
return new Range(this, index, count);
}
public virtual void Sort()
{
Sort(0, Count, Comparer.Default);
}
public virtual void Sort(IComparer comparer)
{
Sort(0, Count, comparer);
}
public virtual void Sort(int index, int count, IComparer comparer)
{
if (index < 0)
{
throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
}
if (count < 0)
{
throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
}
if (_size - index < count)
{
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
}
Array.Sort(_items, index, count, comparer);
_version++;
}
[HostProtection(SecurityAction.LinkDemand, Synchronization = true)]
public static IList Synchronized(IList list)
{
if (list == null)
{
throw new ArgumentNullException("list");
}
return new SyncIList(list);
}
[HostProtection(SecurityAction.LinkDemand, Synchronization = true)]
public static ArrayList Synchronized(ArrayList list)
{
if (list == null)
{
throw new ArgumentNullException("list");
}
return new SyncArrayList(list);
}
public virtual object[] ToArray()
{
object[] array = new object[_size];
Array.Copy(_items, 0, array, 0, _size);
return array;
}
[SecuritySafeCritical]
public virtual Array ToArray(Type type)
{
if (type == null)
{
throw new ArgumentNullException("type");
}
Array array = Array.UnsafeCreateInstance(type, _size);
Array.Copy(_items, 0, array, 0, _size);
return array;
}
public virtual void TrimToSize()
{
Capacity = _size;
}
}
太多了,我后面再一一注释,想用去看源码