自己实现一个内部有增删查改的方法,且能够自动扩容,暴露数组容量和长度的泛型工具类(C#)
class ArrayList<T>
{
private T[] array;
//当前正儿八经存了多少数
private int count;
public ArrayList()
{
count = 0;
//一开始的容量就是16
array = new T[16];
}
//增
public void Add(T value)
{
//是否需要扩容
if( count >= Capacity )
{
//搬家 每次 家扩容2倍
T[] temp = new T[Capacity * 2];
for (int i = 0; i < Capacity; i++)
{
temp[i] = array[i];
}
//重新指向地址
array = temp;
}
//不需要扩容 直接加
array[count] = value;
++count;
}
//删
public void Remove(T value)
{
//这个地方 不是小于数组的容量
//是小于 具体存了几个值
int index = -1;
for (int i = 0; i < Count; i++)
{
//不能用==去判断 因为 不是所有的类型都重载了运算符
if( array[i].Equals(value))
{
index = i;
break;
}
}
//只要不等于-1 就证明找到了 那就去移除
if(index != -1)
{
RemoveAt(index);
}
}
public void RemoveAt(int index)
{
//索引合法不
if( index < 0 || index >= Count )
{
Console.WriteLine("索引不合法");
return;
}
//后面的往前放
for (; index < Count - 1; index++)
{
array[index] = array[index + 1];
}
//把一个数移除了 后面的往前面放 那么最后一个 要移除
array[Count - 1] = default(T);
--count;
}
//查
public T this[int index]
{
get
{
if (index < 0 || index >= Count)
{
Console.WriteLine("索引不合法");
return default(T);
}
return array[index];
}
set
{
if (index < 0 || index >= Count)
{
Console.WriteLine("索引不合法");
return;
}
array[index] = value;
}
}
/// <summary>
/// 获取容量
/// </summary>
public int Capacity
{
get
{
return array.Length;
}
}
/// <summary>
/// 得到具体存了几个值
/// </summary>
public int Count
{
get
{
return count;
}
}
}