146 lines
4.2 KiB
C#
146 lines
4.2 KiB
C#
|
|
using System.Runtime.InteropServices;
|
||
|
|
|
||
|
|
namespace Lesson6_封装_索引器练习
|
||
|
|
{
|
||
|
|
class IntArray
|
||
|
|
{
|
||
|
|
private int[] array;
|
||
|
|
private int used, capacity;
|
||
|
|
public IntArray()
|
||
|
|
{
|
||
|
|
capacity = 5;
|
||
|
|
array = new int[capacity];
|
||
|
|
used = 0;
|
||
|
|
}
|
||
|
|
//增
|
||
|
|
public void Add(int value)
|
||
|
|
{
|
||
|
|
if (used < capacity)
|
||
|
|
{
|
||
|
|
array[used] = value;
|
||
|
|
used++;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
capacity += 1;
|
||
|
|
int[] newArray = new int[capacity];
|
||
|
|
for (int i = 0; i < array.Length; i++)
|
||
|
|
{
|
||
|
|
newArray[i] = array[i];
|
||
|
|
}
|
||
|
|
array = newArray;
|
||
|
|
|
||
|
|
array[used] = value;
|
||
|
|
used++;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
//删
|
||
|
|
public void DelValue(int target)
|
||
|
|
{
|
||
|
|
for (int i = 0; i < used; i++)
|
||
|
|
{
|
||
|
|
if(target == array[i])
|
||
|
|
{
|
||
|
|
DelIndex(i);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
Console.WriteLine("没找到");
|
||
|
|
}
|
||
|
|
public void DelIndex(int target)
|
||
|
|
{
|
||
|
|
if (target > array.Length - 1)
|
||
|
|
{
|
||
|
|
Console.WriteLine("当前数组只有{0}长,溢出",used);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
for (int i = target; i < used-1; i++)
|
||
|
|
{
|
||
|
|
array[i] = array[i + 1];
|
||
|
|
}
|
||
|
|
for (int i = used - 1; i < used; i++)
|
||
|
|
{
|
||
|
|
array[i] = 0;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
--used;
|
||
|
|
|
||
|
|
}
|
||
|
|
public int this[int index]
|
||
|
|
{
|
||
|
|
get
|
||
|
|
{
|
||
|
|
if (index >= capacity)
|
||
|
|
{
|
||
|
|
Console.WriteLine("已溢出,当前index:"+index+'\n'+"当前已用值:"+(used)+"当前容量:"+(capacity));
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
return array[index];
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
set
|
||
|
|
{
|
||
|
|
Console.WriteLine(index);
|
||
|
|
array[index] = value;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
class Program
|
||
|
|
{
|
||
|
|
static void Main(string[] args)
|
||
|
|
{
|
||
|
|
Console.WriteLine("Hello, World!");
|
||
|
|
IntArray a = new IntArray();
|
||
|
|
a.Add(1);
|
||
|
|
a.Add(2);
|
||
|
|
a.Add(3);
|
||
|
|
a.Add(4);
|
||
|
|
a.Add(5);
|
||
|
|
a.Add(6);
|
||
|
|
a.Add(7);
|
||
|
|
a.Add(8);
|
||
|
|
a.Add(9);
|
||
|
|
a.Add(10);
|
||
|
|
Console.WriteLine("dfdf" + a[0]);
|
||
|
|
Console.WriteLine("dfdf" + a[1]);
|
||
|
|
Console.WriteLine("dfdf" + a[2]);
|
||
|
|
Console.WriteLine("dfdf" + a[3]);
|
||
|
|
Console.WriteLine("dfdf" + a[4]);
|
||
|
|
Console.WriteLine("dfdf" + a[5]);
|
||
|
|
Console.WriteLine("dfdf" + a[6]);
|
||
|
|
Console.WriteLine("dfdf" + a[7]);
|
||
|
|
Console.WriteLine("dfdf" + a[8]);
|
||
|
|
Console.WriteLine("dfdf" + a[9]);
|
||
|
|
Console.WriteLine("越界测试" + a[10]);
|
||
|
|
Console.WriteLine();
|
||
|
|
a.DelIndex(3);
|
||
|
|
Console.WriteLine("dfdf" + a[0]);
|
||
|
|
Console.WriteLine("dfdf" + a[1]);
|
||
|
|
Console.WriteLine("dfdf" + a[2]);
|
||
|
|
Console.WriteLine("dfdf" + a[3]);
|
||
|
|
Console.WriteLine("dfdf" + a[4]);
|
||
|
|
Console.WriteLine("dfdf" + a[5]);
|
||
|
|
Console.WriteLine("dfdf" + a[6]);
|
||
|
|
Console.WriteLine("dfdf" + a[7]);
|
||
|
|
Console.WriteLine("dfdf" + a[8]);
|
||
|
|
Console.WriteLine("dfdf" + a[9]);
|
||
|
|
Console.WriteLine();
|
||
|
|
a.DelValue(7);
|
||
|
|
Console.WriteLine("dfdf" + a[0]);
|
||
|
|
Console.WriteLine("dfdf" + a[1]);
|
||
|
|
Console.WriteLine("dfdf" + a[2]);
|
||
|
|
Console.WriteLine("dfdf" + a[3]);
|
||
|
|
Console.WriteLine("dfdf" + a[4]);
|
||
|
|
Console.WriteLine("dfdf" + a[5]);
|
||
|
|
Console.WriteLine("dfdf" + a[6]);
|
||
|
|
Console.WriteLine("dfdf" + a[7]);
|
||
|
|
Console.WriteLine("dfdf" + a[8]);
|
||
|
|
Console.WriteLine("dfdf" + a[9]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|