/// 自定义List
/// </summary>
/// <typeparam name="T">列表中元素的类型</typeparam>
class MyList<T> : IEnumerable
{
T[] t = new T[0];
{
public MyList(int capacity)
{
this.Capacity = capacity;
}
int count = 0;
/// <summary>
/// MyList<T>实际包含的元素个数
/// </summary>
public int Count
{
get { return count; }
/// <summary>
/// 获取或设置MyList<T>的容量
/// </summary>
public int Capacity
{
get { return t.Length; }
set
{
if (value < count)
{
throw new Exception("容量小于元素个数");
}
else
{
T[] newt = new T[value];
for (int i = 0; i < count; i++)
{
newt[i] = t[i];
}
t = newt;
}
}
}
/// <summary>
/// 获取或设置指定索引处的元素
/// </summary>
/// <param name="index">要获得或设置的元素从0开始的索引</param>
/// <returns></returns>
public T this[int index]
{
get
{
if (index >= count)
{
throw new Exception("索引超出范围");
}
else
{
return t[index];
}
}
set
{
if (index >= count)
{
throw new Exception("索引超出范围");
}
else
{
t[index] = value;
}
}
}
/// <summary>
/// foreach循环
/// </summary>
/// <returns></returns>
public IEnumerator GetEnumerator()
{
for (int i = 0; i < count; i++)
{
yield return t[i];
}
}
/// <summary>
/// 将item对象添加到MyList结尾处
/// </summary>
/// <param name="item">item:要添加到MyList结尾处的对象</param>
public void Add(T item)
{
if (t.Length == 0)
{
t = new T[4];
t[0] = item;
count++;
}
else
{
if (count < t.Length)
{
t[count] = item;
count++;
}
else
{
T[] newT = new T[t.Length * 2];
for (int i = 0; i < count; i++)
{
newT[i] = t[i];
}
newT[count] = item;
count++;
t = newT;
}
}
}
/// <summary>
/// 移除特定对象的第一个匹配项
/// </summary>
/// <param name="item">item:要移除的对象</param>
/// <returns></returns>
public bool Remove(T item)
{
int index = -1;
for (int i = 0; i < count; i++)
{
if (t[i].Equals(item))
{
index = i;
break;
}
}
if (index > -1)
{
for (int i = index; i < count; i++)
{
t[i] = t[i + 1];
}
count--;
t[count] = default(T);
return true;
}
else
{
return false;
}
}
/// <summary>
/// 移除特定索引的对象
/// </summary>
/// <param name="index">index:从0开始的索引</param>
public void RemoveAt(int index)
{
if (index < count)
{
for (int i = index; i < count; i++)
{
t[i] = t[i + 1];
}
count--;
}
else
{
throw new Exception("索引大于集合的总个数");
}
}
/// 移除特定索引的对象
/// </summary>
/// <param name="index">index:从0开始的索引</param>
/// <param name="count">count:要删除的个数</param>
public void RemoveRange(int index, int count)
{
if (index + count > this.count || index >= this.count)
{
throw new Exception("点点点");
}
else
{
for (int i = index; i < index + count; i++)
{
t[i] = default(T);
}
this.count -= count;
}
}
/// <summary>
/// 在特定的位置插入对象
/// </summary>
/// <param name="index">index:从0开始的索引</param>
/// <param name="item">item:插入的对象</param>
public void Insert(int index, T item)
{
if (index > count)
{
throw new Exception("超出索引界限");
}
else
{
for (int i = count; i > index; i--)
{
t[i] = t[i - 1];
}
t[index] = item;
count++;
}
}
/// <summary>
/// 返回第一个匹配项的位置
/// </summary>
/// <param name="item">item:要匹配的项</param>
/// <returns></returns>
public int IndexOf(T item)
{
int num = -1;
for (int i = 0; i < count; i++)
{
if (t[i].Equals(item))
{
num = i;
break;
}
}
return num;
}
/// <summary>
/// 返回第一个匹配项的位置
/// </summary>
/// <param name="item">item:要匹配的项</param>
/// <param name="index">index:从index开始索引</param>
/// <returns></returns>
public int IndexOf(T item, int index)
{
int num = -1;
if (index < count)
{
for (int i = index; i < count; i++)
{
if (t[i].Equals(item))
{
num = i;
break;
}
}
}
else
{
throw new Exception("索引大于MyList<T>元素的总个数");
}
return num;
}
/// <summary>
/// 返回第一个匹配项的位置
/// </summary>
/// <param name="item">item:要匹配的项</param>
/// <param name="index">index:从index开始索引</param>
/// <param name="count">count:从index开始索引查找几个索引</param>
/// <returns></returns>
public int IndexOf(T item, int index, int count)
{
int num = -1;
if (index + count > this.count)
{
throw new Exception("index+count的和不能大于MyList<T>元素的总个数");
}
else
{
for (int i = index; i < index + count; i++)
{
if (t[i].Equals(item))
{
num = i;
break;
}
}
}
return num;
}
/// <summary>
/// 查找最后一个匹配项的位置
/// </summary>
/// <param name="item">item:要匹配的项</param>
/// <returns></returns>
public int LastIndexOf(T item)
{
int num = -1;
for (int i = count - 1; i >= 0; i--)
{
if (t[i].Equals(item))
{
num = i;
break;
}
}
return num;
}
/// <summary>
/// 查找从index索引开始的最后一个匹配项的位置
/// </summary>
/// <param name="item">item:要匹配的项</param>
/// <param name="index">index:从该索引处开始查找</param>
/// <returns></returns>
public int LastIndexOf(T item, int index)
{
int num = -1;
if (index < count && index > -1)
{
for (int i = index; i >= 0; i--)
{
if (t[i].Equals(item))
{
num = i;
break;
}
}
}
else
{
throw new Exception("索引值超过MyList<T>集合界限");
}
return num;
}
/// <summary>
/// 查找从index索引开始的最后一个匹配项的位置
/// </summary>
/// <param name="item">item:要匹配的项</param>
/// <param name="index">index:从该索引处开始查找</param>
/// <param name="count">count:查找几个对象</param>
/// <returns></returns>
public int LastIndexOf(T item, int index, int count)
{
int num = -1;
if (index < this.count && index - count >= -1 && count >= 0)
{
for (int i = index; i >= index - count; i--)
{
if (t[i].Equals(item))
{
num = i;
break;
}
}
}
else
{
throw new Exception("索引值超过MyList<T>集合界限");
}
return num;
}
/// <summary>
/// 反转
/// </summary>
public void Reverse()
{
T[] newT = new T[count];
int j = 0;
for (int i = count - 1; i >= 0; i--)
{
newT[j] = t[i];
j++;
}
t = newT;
}
/// <summary>
/// 反转
/// </summary>
/// <param name="index">index:从该索引出开始反转</param>
/// <param name="count">count:要反转对象的个数</param>
public void Reverse(int index, int count)
{
if (index > this.count || index + count > this.count)
{
throw new Exception("index或index+count的和超过集合元素的总个数");
}
else if (index < 0 || count < 0)
{
throw new Exception("不能为负数");
}
else
{
T[] newT = new T[count];
int j = 0;
for (int i = count + index - 1; i >= index; i--)
{
newT[j] = t[i];
j++;
}
for (int i = 0; i < count; i++)
{
t[index + i] = newT[i];
}
}
}
/// <summary>
/// 清空
/// </summary>
public void Clear()
{
for (int i = count - 1; i >= 0; i--)
{
t[i] = default(T);
}
count = 0;
}
/// <summary>
/// 是否包含item该对象
/// </summary>
/// <param name="item">item:是否包含的对象</param>
/// <returns></returns>
public bool Contains(T item)
{
bool sign = false;
foreach (T tt in t)
{
if (tt.Equals(item))
{
sign = true;
break;
}
}
return sign;
}
}