class List
{
//目标集合
int[] numArr;
//接收集合再赋值时的数组
int[] arr;
//集合能容纳的最大长度
private int Capcity;
//集合当前所有元素的个数
private int count;
//让外界可以查看集合目前的元素个数
public int Count
{
get
{
return count;
}
}
//让外界可以查看当前集合可以容纳的最大元素个数
public int Capcity1 { get => Capcity; set => Capcity = value; }
//集合的初始化
public List()
{
Capcity1 = 0;
count = 0;
numArr = new int[Capcity1];
}
//集合的下标索引器
public int this[int index]
{
get
{
if (index < 0)
{
return -1;
}
else if (index > count - 1)
{
return -1;
}
return numArr[index];
}
set
{
numArr[index] = value;
}
}
//添加单个元素的方法
public void Add(int num)
{
count++;
if (Capcity1 == 0)
{
Capcity1 = Capcity1 + 1;
arr = new int[Capcity1];
for (int i = 0; i < numArr.Length; i++)
{
arr[i] = numArr[i];
}
numArr = new int[Capcity1];
for (int i = 0; i < Capcity1; i++)
{
numArr[i] = arr[i];
}
}
else if (count >= Capcity1 && Capcity1 != 0)
{
Capcity1 *= 4;
arr = new int[Capcity1];
for (int i = 0; i < numArr.Length; i++)
{
arr[i] = numArr[i];
}
numArr = new int[Capcity1];
for (int i = 0; i < Capcity1 / 4; i++)
{
numArr[i] = arr[i];
}
}
for (int i = count - 1; i < count; i++)
{
numArr[i] = num;
}
}
//获取要删除元素原本在集合中的第一个下标
public int _index;
//删除集合中的特定元素的第一个匹配项
public void Remove(int num)
{
int r = 0;
for (int i = 0; i < count; i++)
{
if (numArr[i] != num)
{
r++;
}
}
if (count == 0||r>=count)
{
Console.WriteLine("false");
}
else if (count >0&&r<count)
{
for (int i = 0; i < count; i++)
{
if(numArr[i] == num)
{
_index = i;
break;
}
}
count--;
arr = numArr;
for (int i = 0; i < count; i++)
{
if (i >= _index)
{
numArr[i] = arr[i+1];
}
else
{
numArr[i] = arr[i];
}
}
}
}
int arrLen;
int T;
//添加数组的方法
public void AddRange(int[] Arr)
{
if (count > Arr.Length)
{
count += Arr.Length;
arrLen = count - Arr.Length ;
T = 2;
}
else if (count == Arr.Length&&Arr.Length!=0)
{
count += Arr.Length;
arrLen = Arr.Length;
T = 1;
}
else if (count < Arr.Length)
{
count += Arr.Length;
arrLen = count - Arr.Length;
T = 0;
}
if (Capcity1 == 0)
{
Capcity1 = Arr.Length;
arr = new int[Capcity1];
for (int i = 0; i < numArr.Length; i++)
{
arr[i] = numArr[i];
}
numArr = new int[Capcity1];
for (int i = 0; i < Capcity1; i++)
{
numArr[i] = arr[i];
}
}
else if (count>= Capcity1&&Capcity1!=0)
{
Capcity1 = (Arr.Length + Capcity1) * 4;
arr = new int[Capcity1];
for (int i = 0; i < numArr.Length; i++)
{
arr[i] = numArr[i];
}
numArr = new int[Capcity1];
for (int i = 0; i < Capcity1 / 4; i++)
{
numArr[i] = arr[i];
}
}
if (T == 2)
{
for (int i = count - Arr.Length ; i < count; i++)//4
{
numArr[i] = Arr[i - arrLen];//0.1.2.3.4
}
}
else if (T == 1)
{
for (int i = Arr.Length; i < count; i++)
{
numArr[i] = Arr[i - arrLen];
}
}
else if (T == 0)
{
for (int i = arrLen; i < count; i++)
{
numArr[i] = Arr[i - arrLen];
}
}
}
//删除一定范围内的集合元素
public void RemoveRange(int index,int max)
{
if (index >= 0 && max <= count && max > index)
{
count = count - max + index;
arr = numArr;
for (int i = 0; i < count; i++)
{
if (i >= index )
{
numArr[i] = arr[i - index + max];
}
else
{
numArr[i] = arr[i];
}
}
}
else
{
Console.WriteLine("你添加的索引有问题!");
}
}
C#基础,制作一个有存储和删减元素和数组的int型集合
最新推荐文章于 2023-12-13 16:55:25 发布