主要实现动态扩容动态缩容,增删改查的底层实现。代码有注释。
可以直接复制类去跑。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataStucte
{
class Array1
{
private int[] Data;
private int N; //存在的元素个数
/// <summary>
/// 初始化构造函数
/// </summary>
/// <param name="capacity">初始化内存</param>
public Array1(int capacity)
{
Data = new int[capacity];
N = 0;
}
//默认10个容量
public Array1() : this(10)
{
}
//数组长度
public int Capacity {
get { return Data.Length; }
}
//存储元素的多少
public int Count {
get { return N; }
}
//判断动态数组是否为空
public bool IsEmpty
{
get { return N == 0; }
}
//添加元素
public void Add(int index,int e)
{
if (index < 0 || index > N)
{
throw new ArgumentException("数组索引越界");
}
if (N == Data.Length)
{ //扩容
ResetCapacity(2 * Data.Length);
}
for (int i = N - 1; i >= index; i--)
{
Data[i + 1] = Data[i];
}
Data[index] = e;
N++;
}
//向尾部添加
public void AddLast(int e)
{
Add(N, e);
}
//向头添加
public void AddFirst(int e)
{
Add(0, e);
}
//获指定索引元素
public int Get(int index)
{
if (index < 0 || index >= N)
{
throw new ArgumentException("数组索引越界");
}
return Data[index];
}
/// <summary>
/// 获取数组中的第一个元素
/// </summary>
/// <returns></returns>
public int GetFirst()
{
return Get(0);
}
/// <summary>
/// 获取最后一个元素
/// </summary>
/// <returns></returns>
public int GetLast()
{
return Get(N - 1);
}
/// <summary>
/// 修改指定下标赋值
/// </summary>
/// <param name="index">下标</param>
/// <param name="newE">值</param>
public void Set(int index, int newE)
{
if (index < 0 || index >= N)
{
throw new ArgumentException("数组索引越界");
}
Data[index] = newE;
}
/// <summary>
/// 查看元素是否存在
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
public bool Contains(int e)
{
for (int i = 0; i < N; i++)
{
if (Data[i] == e)
{
return true;
}
}
return false;
}
/// <summary>
/// 返回元素位置
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
public int IndexOf(int n)
{
for (int i = 0; i < N; i++)
{
if (Data[i] == n)
{
return i;
}
}
return -1;
}
/// <summary>
/// 删除指定位置元素
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public int RemoveAt(int index)
{
if (index < 0 || index >= N)
{
throw new ArgumentException("索引超出了数组界限");
}
int del = Data[index];
for (int i = index+1; i <=N-1; i++)
{
Data[i - 1] = Data[i];
}
N--;
Data[N] = default(int);
//缩容操作 当元素个数等于容量的四分之一的时候就进行缩容操作
if (N == Data.Length / 4)
ResetCapacity(Data.Length / 2);
return del;
}
public int RemoveFirst()
{
return RemoveAt(0);
}
public int RemoveLast()
{
return RemoveAt(N-1);
}
//删除指定元素
public void Remove(int e)
{
int index = IndexOf(e);
if (index != -1)
RemoveAt(index);
}
/// <summary>
/// 容量扩容
/// </summary>
/// <param name="newCapacity"></param>
private void ResetCapacity(int newCapacity)
{
int[] newDate = new int[newCapacity];
for (int i = 0; i < N; i++)
{
newDate[i] = Data[i];
}
Data = newDate;
}
public override string ToString()
{
StringBuilder res = new StringBuilder();
res.Append(string.Format("Array1: count={0} capacity={1}\n",N,Data.Length));
res.Append("[");
for (int i = 0; i < N; i++)
{
res.Append(Data[i]);
if (i != N - 1)
{
res.Append(", ");
}
}
res.Append("]");
return res.ToString();
}
}
}