class Program
{
static void Main(string[] args)
{
FanxingList<int> mylist = new FanxingList<int>();
for (int i = 0; i < 10; i++)
{
mylist.Add(i);
}
mylist.Insert(1, 88);
for (int i = 0; i < mylist.Count; i++)
{
Console.Write(mylist[i] + " ");
}
mylist.IndexOf(1, 2, 3, 4);
Console.ReadKey();
}
}
class FanxingList<T>
{
private T[] arr;
private int count;
public int Count
{
get { return this.count; }
set { count = value; }
}
public int Capactiy
{
get { return arr.Length; }
}
public FanxingList()
{
arr = new T[] { };
}
public FanxingList(int length)
{
if (length >= 0)
{
arr = new T[length];
}
else
{
Console.WriteLine("不能為負(fù)數(shù)唆垃!");
}
}
public void Add(T item)
{
if (this.count == this.Capactiy)
{
if (this.count == 0)//當(dāng)?shù)扔? 的時(shí)候擴(kuò)容,創(chuàng)建一個(gè)4的長(zhǎng)度數(shù)組览妖;
{
arr = new T[4];
}
if (this.count == this.Capactiy && this.count != 0)//擴(kuò)容操作
{
T[] arrNew = new T[this.Capactiy * 2];//創(chuàng)建擴(kuò)容數(shù)組
Array.Copy(arr, arrNew, this.count);//把原來數(shù)組copy到擴(kuò)容后的數(shù)組;
arr = arrNew;
}
}
arr[this.Count] = item;
count++;
}
/// <summary>
/// 設(shè)置索引器
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public T this[int index]
{
get { return this.GetItem(index); }
set
{
if (index >= 0 && index < this.Capactiy)
{
arr[index] = value;
}
else
{
throw new Exception("索引產(chǎn)出范圍汁针!");
}
}
}
private T GetItem(int index)
{
if (index >= 0 && index < this.Capactiy)
{
return arr[index];
}
else
{
throw new Exception("索引產(chǎn)出范圍华望!");
}
}
//插入一個(gè)元素
public void Insert(int index, T item)
{
if (this.count + 1 > this.Capactiy)
{
T[] arrNew = new T[this.Capactiy * 2];//創(chuàng)建擴(kuò)容數(shù)組
Array.Copy(arr, arrNew, this.count);//把原來數(shù)組copy到擴(kuò)容后的數(shù)組檩互;
arr = arrNew;
for (int i = 0; i < this.count; i++)
{
if (i >= index)
{
for (int j = this.count; j >= index; j--)
{
arr[j] = arr[j - 1];
}
arr[index] = item;
count++;
break;
}
}
}
else
{
for (int i = 0; i < this.count; i++)
{
if (i >= index)
{
for (int j = this.count; j >= index; j--)
{
arr[j] = arr[j - 1];
}
this.count++;
arr[index] = item;
break;
}
}
}
}
//指定下標(biāo)移除元素
public void RemoveAt(int index)
{
for (int i = 0; i < this.count; i++)
{
if (i == index)
{
for (int j = index; j < this.count; j++)
{
arr[j] = arr[j + 1];
}
this.count--;
break;
}
}
}
//指定元素尋找第一次出現(xiàn)的下標(biāo)
public int IndexOf(T str)
{
int index = -1;
for (int i = 0; i < arr.Length; i++)
{
if (arr[i].Equals(str))//判斷是否相等
{
index = i;
}
}
return index;
}
//反轉(zhuǎn)
public void Reverse()
{
T[] arrTemp = new T[this.count];
for (int i = 0; i < this.count; i++)
{
arrTemp[i] = arr[this.count - 1 - i];
}
arr = arrTemp;
}
//插入
public void Insert(int index, T item)
{
if (this.Count == 0)
{
arr = new T[4];
}
if (index >= 0 && index < this.Capactiy)
{
T[] arrNew = new T[this.Capactiy * 2];//創(chuàng)建擴(kuò)容數(shù)組
Array.Copy(arr, arrNew, this.count);//把原來數(shù)組copy到擴(kuò)容后的數(shù)組如迟;
arr = arrNew;
for (int i = 0; i < this.count; i++)
{
if (i >= index)
{
for (int j = this.count - 1; j >= index; j--)
{
arr[j + 1] = arr[j];
}
arr[index] = item;
count++;
break;
}
}
}
else
{
for (int i = 0; i < this.count; i++)
{
if (i >= index)
{
for (int j = this.count - 1; j >= index; j--)
{
arr[j + 1] = arr[j];
}
this.count++;
arr[index] = item;
break;
}
}
}
}
public int IndexOf(T item, int index, int _count)
{ //有后面兩個(gè)參數(shù)
for (int i = index; i <= index + _count; i++)
{
if (arr[i].Equals(item))
{
return i;
}
}
return -1;
}
public int IndexOf(T item)//重載 無后面參數(shù)
{
for (int i = 0; i < this.Count; i++)
{
if (arr[i].Equals(item))
{
return i;
}
}
return -1;
}
public int IndexOf(T item, params int[] Canshu)
{
if (Canshu.Length == 2)
{
for (int i = Canshu[0]; i <= Canshu[0] + Canshu[1]; i++)
{
if (arr[i].Equals(item))
{
return i;
}
}
}
else if (Canshu.Length == 0)
{
for (int i = 0; i < this.Count; i++)
{
if (arr[i].Equals(item))
{
return i;
}
}
}
else
{
Console.WriteLine("傳入?yún)?shù)不正確下隧!");
}
return -1;
}
public int LastIndexOf(T item)
{
int index = -1;
for (int i = this.Count; i >= 0; i--)
{
if (arr[i].Equals(item))
{
return i;
}
}
return index;
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者