對於ArrayList中如果插入值類型會引發裝箱操作,而取出值類型又需要拆箱,如下
[csharp]
ArrayList myArrayList = new ArrayList();
myArrayList.Add(40);//裝箱
myArrayList.Add(80);//裝箱
Int32 a1 = (Int32)myArrayList[0];//拆箱
Int32 a2 = (Int32)myArrayList[1];//拆箱
ArrayList myArrayList = new ArrayList();
myArrayList.Add(40);//裝箱
myArrayList.Add(80);//裝箱
Int32 a1 = (Int32)myArrayList[0];//拆箱
Int32 a2 = (Int32)myArrayList[1];//拆箱從而造成性能的消耗。至於裝箱的詳細解說見下一篇。
為了解決這些問題,C#中有支持泛型的IList<T>接口,下面看詳細代碼,其實結構都和IList一樣,只是增加了泛型。
[csharp]
/// <summary>
/// 泛型集合類
/// </summary>
/// <typeparam name="T"></typeparam>
public class List<T> : IList<T>, IList
{
/// <summary>
/// 泛型迭代器
/// </summary>
/// <typeparam name="T"></typeparam>
public struct Enumertor<T> : IEnumerator, IEnumerator<T>
{
//迭代索引
private int index;
//迭代器所屬的集合對象引用
private List<T> list;
public Enumertor(List<T> container)
{
this.list = container;
this.index = -1;
}
public void Dispose()
{
}
/// <summary>
/// 顯示實現IEnumerator的Current屬性
/// </summary>
object IEnumerator.Current
{
get
{
return list[index];
}
}
/// <summary>
/// 實現IEnumerator<T>的Current屬性
/// </summary>
public T Current
{
get
{
return list[index];
}
}
/// <summary>
/// 迭代器指示到下一個數據位置
/// </summary>
/// <returns></returns>
public bool MoveNext()
{
if (this.index < list.Count)
{
++this.index;
}
return this.index < list.Count;
}
public void Reset()
{
this.index = -1;
}
}
/// <summary>
/// 保存數據的數組,T類型則體現了泛型的作用。
/// </summary>
private T[] array;
/// <summary>
/// 當前集合的長度
/// </summary>
private int count;
/// <summary>
/// 默認構造函數
/// </summary>
public List()
: this(1)
{
}
public List(int capacity)
{
if (capacity < 0)
{
throw new Exception("集合初始長度不能小於0");
}
if (capacity == 0)
{
capacity = 1;
}
this.array = new T[capacity];
}
/// <summary>
/// 集合長度
/// </summary>
public int Count
{
get
{
return this.count;
}
}
/// <summary>
/// 集合實際長度
/// </summary>
public int Capacity
{
get
{
return this.array.Length;
}
}
/// <summary>
/// 是否固定大小
/// </summary>
public bool IsFixedSize
{
get
{
return false;
}
}
/// <summary>
/// 是否只讀
/// </summary>
public bool IsReadOnly
{
get
{
return false;
}
}
/// <summary>
/// 是否可同屬性
/// </summary>
public bool IsSynchronized
{
get
{
return false;
}
}
/// <summary>
/// 同步對象
/// </summary>
public object SyncRoot
{
get
{
return null;
}
}
/// <summary>
/// 長度不夠時,重新分配長度足夠的數組
/// </summary>
/// <returns></returns>
private T[] GetNewArray()
{
return new T[(this.array.Length + 1) * 2];
}
/// <summary>
/// 實現IList<T>Add方法
/// </summary>
/// <param name="value"></param>
public void Add(T value)
{
int newCount = this.count + 1;
if (this.array.Length < newCount)
{
T[] newArray = GetNewArray();
Array.Copy(this.array, newArray, this.count);
this.array = newArray;
}
this.array[this.count] = value;
this.count = newCount;
}
/// <summary>
/// 向集合末尾添加對象
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
int IList.Add(object value)
{
((IList<T>)this).Add((T)value);
return this.count - 1;
}
/// <summary>
/// 實現IList<T>索引器
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public T this[int index]
{
get
{
if (index < 0 || index >= this.count)
{
throw new ArgumentOutOfRangeException("index");
}
return this.array[index];
}
set
{
if (index < 0 || index >= this.count)
{
throw new ArgumentOutOfRangeException("index");
}
this.array[index] = value;
}
}
/// <summary>
/// 顯示實現IList接口的索引器
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
object IList.this[int index]
{
get
{
return ((IList<T>)this)[index];
}
set
{
((IList<T>)this)[index] = (T)value;
}
}
/// <summary>
/// 刪除集合中的元素
/// </summary>
/// <param name="index"></param>
/// <param name="count"></param>
public void RemoveRange(int index, int count)
{
if (index < 0)
{
throw new ArgumentOutOfRangeException("index");
}
int removeIndex = index + count;
if (count < 0 || removeIndex > this.count)
{
throw new ArgumentOutOfRangeException("index");
}
Array.Copy(this.array, index + 1, this.array, index + count - 1, this.count - removeIndex);
this.count -= count;
}
/// <summary>
/// 實現IList<T>接口的indexOf方法
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public int IndexOf(T value)
{
int index = 0;
if (value == null)
{
while (index < this.count)
{
if (this.array[index] == null)
{
return index;
}
++index;
}
}
else
{
while (index < this.count)
{
if (value.Equals(this.array[index]))
{
return index;
}
++index;
}
}
return -1;
}
/// <summary>
/// 顯示實現IList接口的IndexOf方法
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
int IList.IndexOf(object value)
{
return ((IList<T>)this).IndexOf((T)value);
}
/// <summary>
/// 查找對應數組項
/// </summary>
/// <param name="o"></param>
/// <param name="compar"></param>
/// <returns></returns>
public int IndexOf(object o, IComparer compar)
{
int index = 0;
while (index < this.count)
{
if (compar.Compare(this.array[index], o) == 0)
{
return index;
}
++index;
}
return -1;
}
/// <summary>
/// 實現IList<T>接口的Remove方法
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public bool Remove(T value)
{
int index = this.IndexOf(value);
if (index >= 0)
{
this.RemoveRange(index, 1);
return true;
}
return false;
}
/// <summary>
/// 顯示實現IList接口的Remove方法,此處顯示實現
/// </summary>
/// <param name="value"></param>
void IList.Remove(object value)
{
((IList<T>)this).Remove((T)value);
}
/// <summary>
/// 從集合指定位置刪除對象的引用
/// </summary>
/// <param name="index"></param>
public void RemoveAt(int index)
{
RemoveRange(index, 1);
}
/// <summary>
/// 彈出集合的最後一個元素
/// </summary>
/// <returns></returns>
public object PopBack()
{
object o = this.array[this.count - 1];
RemoveAt(this.count - 1);
return o;
}
/// <summary>
/// 彈出集合第一個對象
/// </summary>
/// <returns></returns>
public object PopFront()
{
object o = this.array[0];
RemoveAt(0);
return o;
}
/// <summary>
/// 實現IList<T>接口的Insert方法
/// </summary>
/// <param name="index"></param>
/// <param name="value"></param>
public void Insert(int index, T value)
{
if (index >= this.count)
{
throw new ArgumentOutOfRangeException("index");
}
int newCount = this.count + 1;
if (this.array.Length < newCount)
{
T[] newArray = GetNewArray();
Array.Copy(this.array, newArray, index);
newArray[index] = value;
Array.Copy(this.array, index, newArray, index + 1, this.count - index);
this.array = newArray;
}
else
{
Array.Copy(this.array, index, this.array, index + 1, this.count - index);
this.array[index] = value;
}
this.count = newCount;
}
/// <summary>
/// 顯示實現IList接口的Insert方法
/// </summary>
/// <param name="index"></param>
/// <param name="value"></param>
void IList.Insert(int index, object value)
{
((IList<T>)this).Insert(index, (T)value);
}
/// <summary>
/// 實現IList<T>接口的Contains方法
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public bool Contains(T value)
{
return this.IndexOf(value) >= 0;
}
/// <summary>
/// 顯示實現IList<T>接口的Contains方法
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
bool IList.Contains(object value)
{
return ((IList<T>)this).IndexOf((T)value) >= 0;
}
/// <summary>
/// 將集合壓縮為實際長度
/// </summary>
public void TrimToSize()
{
if (this.array.Length > this.count)
{
T[] newArray = null;
if (this.count > 0)
{
newArray = new T[this.count];
Array.Copy(this.array, newArray, this.count);
}
else
{
newArray = new T[1];
}
this.array = newArray;
}
}
/// <summary>
/// 清空集合
/// </summary>
public void Clear()
{
this.count = 0;
}
/// <summary>
/// 實現IEnumerable接口的GetEnumerator方法
/// </summary>
/// <returns></returns>
public IEnumerator<T> GetEnumerator()
{
Enumertor<T> ator = new Enumertor<T>(this);
return ator;
}
/// <summary>
/// 顯示實現IEnumerable接口的GetEnumerator方法
/// </summary>
/// <returns></returns>
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<T>)this).GetEnumerator();
}
/// <summary>
/// 實現ICollection<T>接口的CopyTo方法
/// </summary>
/// <param name="array"></param>
/// <param name="index"></param>
public void CopyTo(T[] array, int index)
{
Array.Copy(this.array, 0, array, index, this.count);
}
/// <summary>
/// 顯示實現實現ICollection<T>接口的CopyTo方法
/// </summary>
/// <param name="array"></param>
/// <param name="index"></param>
void ICollection.CopyTo(Array array, int index)
{
Array.Copy(this.array, 0, array, index, this.count);
}
}
/// <summary>
/// 泛型集合類
/// </summary>
/// <typeparam name="T"></typeparam>
public class List<T> : IList<T>, IList
{
/// <summary>
/// 泛型迭代器
/// </summary>
/// <typeparam name="T"></typeparam>
public struct Enumertor<T> : IEnumerator, IEnumerator<T>
{
//迭代索引
private int index;
//迭代器所屬的集合對象引用
private List<T> list;
public Enumertor(List<T> container)
{
this.list = container;
this.index = -1;
}
public void Dispose()
{
}
/// <summary>
/// 顯示實現IEnumerator的Current屬性
/// </summary>
object IEnumerator.Current
{
get
{
return list[index];
}
}
/// <summary>
/// 實現IEnumerator<T>的Current屬性
/// </summary>
public T Current
{
get
{
return list[index];
}
}
/// <summary>
/// 迭代器指示到下一個數據位置
/// </summary>
/// <returns></returns>
public bool MoveNext()
{
if (this.index < list.Count)
{
++this.index;
}
return this.index < list.Count;
}
public void Reset()
{
this.index = -1;
}
}
/// <summary>
/// 保存數據的數組,T類型則體現了泛型的作用。
/// </summary>
private T[] array;
/// <summary>
/// 當前集合的長度
/// </summary>
private int count;
/// <summary>
/// 默認構造函數
/// </summary>
public List()
: this(1)
{
}
public List(int capacity)
{
if (capacity < 0)
{
throw new Exception("集合初始長度不能小於0");
}
if (capacity == 0)
{
capacity = 1;
}
this.array = new T[capacity];
}
/// <summary>
/// 集合長度
/// </summary>
public int Count
{
get
{
return this.count;
}
}
/// <summary>
/// 集合實際長度
/// </summary>
public int Capacity
{
get
{
return this.array.Length;
}
}
/// <summary>
/// 是否固定大小
/// </summary>
public bool IsFixedSize
{
get
{
return false;
}
}
/// <summary>
/// 是否只讀
/// </summary>
public bool IsReadOnly
{
get
{
return false;
}
}
/// <summary>
/// 是否可同屬性
/// </summary>
public bool IsSynchronized
{
get
{
return false;
}
}
/// <summary>
/// 同步對象
/// </summary>
public object SyncRoot
{
get
{
return null;
}
}
/// <summary>
/// 長度不夠時,重新分配長度足夠的數組
/// </summary>
/// <returns></returns>
private T[] GetNewArray()
{
return new T[(this.array.Length + 1) * 2];
}
/// <summary>
/// 實現IList<T>Add方法
/// </summary>
/// <param name="value"></param>
public void Add(T value)
{
int newCount = this.count + 1;
if (this.array.Length < newCount)
{
T[] newArray = GetNewArray();
Array.Copy(this.array, newArray, this.count);
this.array = newArray;
}
this.array[this.count] = value;
this.count = newCount;
}
/// <summary>
/// 向集合末尾添加對象
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
int IList.Add(object value)
{
((IList<T>)this).Add((T)value);
return this.count - 1;
}
/// <summary>
/// 實現IList<T>索引器
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public T this[int index]
{
get
{
if (index < 0 || index >= this.count)
{
throw new ArgumentOutOfRangeException("index");
}
return this.array[index];
}
set
{
if (index < 0 || index >= this.count)
{
throw new ArgumentOutOfRangeException("index");
}
this.array[index] = value;
}
}
/// <summary>
/// 顯示實現IList接口的索引器
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
object IList.this[int index]
{
get
{
return ((IList<T>)this)[index];
}
set
{
((IList<T>)this)[index] = (T)value;
}
}
/// <summary>
/// 刪除集合中的元素
/// </summary>
/// <param name="index"></param>
/// <param name="count"></param>
public void RemoveRange(int index, int count)
{
if (index < 0)
{
throw new ArgumentOutOfRangeException("index");
}
int removeIndex = index + count;
if (count < 0 || removeIndex > this.count)
{
throw new ArgumentOutOfRangeException("index");
}
Array.Copy(this.array, index + 1, this.array, index + count - 1, this.count - removeIndex);
this.count -= count;
}
/// <summary>
/// 實現IList<T>接口的indexOf方法
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public int IndexOf(T value)
{
int index = 0;
if (value == null)
{
while (index < this.count)
{
if (this.array[index] == null)
{
return index;
}
++index;
}
}
else
{
while (index < this.count)
{
if (value.Equals(this.array[index]))
{
return index;
}
++index;
}
}
return -1;
}
/// <summary>
/// 顯示實現IList接口的IndexOf方法
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
int IList.IndexOf(object value)
{
return ((IList<T>)this).IndexOf((T)value);
}
/// <summary>
/// 查找對應數組項
/// </summary>
/// <param name="o"></param>
/// <param name="compar"></param>
/// <returns></returns>
public int IndexOf(object o, IComparer compar)
{
int index = 0;
while (index < this.count)
{
if (compar.Compare(this.array[index], o) == 0)
{
return index;
}
++index;
}
return -1;
}
/// <summary>
/// 實現IList<T>接口的Remove方法
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public bool Remove(T value)
{
int index = this.IndexOf(value);
if (index >= 0)
{
this.RemoveRange(index, 1);
return true;
}
return false;
}
/// <summary>
/// 顯示實現IList接口的Remove方法,此處顯示實現
/// </summary>
/// <param name="value"></param>
void IList.Remove(object value)
{
((IList<T>)this).Remove((T)value);
}
/// <summary>
/// 從集合指定位置刪除對象的引用
/// </summary>
/// <param name="index"></param>
public void RemoveAt(int index)
{
RemoveRange(index, 1);
}
/// <summary>
/// 彈出集合的最後一個元素
/// </summary>
/// <returns></returns>
public object PopBack()
{
object o = this.array[this.count - 1];
RemoveAt(this.count - 1);
return o;
}
/// <summary>
/// 彈出集合第一個對象
/// </summary>
/// <returns></returns>
public object PopFront()
{
object o = this.array[0];
RemoveAt(0);
return o;
}
/// <summary>
/// 實現IList<T>接口的Insert方法
/// </summary>
/// <param name="index"></param>
/// <param name="value"></param>
public void Insert(int index, T value)
{
if (index >= this.count)
{
throw new ArgumentOutOfRangeException("index");
}
int newCount = this.count + 1;
if (this.array.Length < newCount)
{
T[] newArray = GetNewArray();
Array.Copy(this.array, newArray, index);
newArray[index] = value;
Array.Copy(this.array, index, newArray, index + 1, this.count - index);
this.array = newArray;
}
else
{
Array.Copy(this.array, index, this.array, index + 1, this.count - index);
this.array[index] = value;
}
this.count = newCount;
}
/// <summary>
/// 顯示實現IList接口的Insert方法
/// </summary>
/// <param name="index"></param>
/// <param name="value"></param>
void IList.Insert(int index, object value)
{
((IList<T>)this).Insert(index, (T)value);
}
/// <summary>
/// 實現IList<T>接口的Contains方法
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public bool Contains(T value)
{
return this.IndexOf(value) >= 0;
}
/// <summary>
/// 顯示實現IList<T>接口的Contains方法
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
bool IList.Contains(object value)
{
return ((IList<T>)this).IndexOf((T)value) >= 0;
}
/// <summary>
/// 將集合壓縮為實際長度
/// </summary>
public void TrimToSize()
{
if (this.array.Length > this.count)
{
T[] newArray = null;
if (this.count > 0)
{
newArray = new T[this.count];
Array.Copy(this.array, newArray, this.count);
}
else
{
newArray = new T[1];
}
this.array = newArray;
}
}
/// <summary>
/// 清空集合
/// </summary>
public void Clear()
{
this.count = 0;
}
/// <summary>
/// 實現IEnumerable接口的GetEnumerator方法
/// </summary>
/// <returns></returns>
public IEnumerator<T> GetEnumerator()
{
Enumertor<T> ator = new Enumertor<T>(this);
return ator;
}
/// <summary>
/// 顯示實現IEnumerable接口的GetEnumerator方法
/// </summary>
/// <returns></returns>
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<T>)this).GetEnumerator();
}
/// <summary>
/// 實現ICollection<T>接口的CopyTo方法
/// </summary>
/// <param name="array"></param>
/// <param name="index"></param>
public void CopyTo(T[] array, int index)
{
Array.Copy(this.array, 0, array, index, this.count);
}
/// <summary>
/// 顯示實現實現ICollection<T>接口的CopyTo方法
/// </summary>
/// <param name="array"></param>
/// <param name="index"></param>
void ICollection.CopyTo(Array array, int index)
{
Array.Copy(this.array, 0, array, index, this.count);
}
}調用:
[csharp]
static void Main(string[] args)
{
//由於已經指定了int,因此加入值類型不會有裝箱拆箱操作。
List<int> tList = new List<int>();
tList.Add(25);
tList.Add(30);
foreach (int n in tList)
{
Console.WriteLine(n);
}
Console.ReadLine();