class LinerArr
{
public int count = 0;
public T[] arr;
private int capacity = 30;
public LinerArr()
{
arr = new T[capacity];
count = 0;
}
public void Add(T item)
{
arr[count] = item;
count++;
}
public void Insert(int index,T item)
{
if(index>=capacity||index >count)
{
Console.WriteLine("超出最大内存空间");
return;
}
arr[index] = item;
count++;
for (int i = count; i>=index; i--)
{
arr[i+1] = arr[i];
}
}
public void Delete(int index)
{
for (int i = index; i < count; i++)
{
arr[i] = arr[i + 1];
}
count--;
}
public T this[int index]
{
get
{
return arr[index];
}
}
}
class Program
{
static void Main(string[] args)
{
LinerArr<int> arr = new LinerArr<int>();
for (int i = 10; i < 21; i++)
{
arr.Add(i);
}
Console.Write("遍历所有元素");
for (int i = 0; i < arr.count; i++)
{
Console.Write(arr[i]+" ");
}
Console.WriteLine();
arr.Insert(11, 60);
Console.Write("插入后的数据:");
for (int i = 0; i < arr.count; i++)
{
Console.Write(arr[i] + " ");
}
Console.WriteLine();
arr.Delete(10);
Console.Write("删除后的数据:");
for (int i = 0; i < arr.count; i++)
{
Console.Write(arr[i] + " ");
}
Console.ReadKey();
}
}