日期:2014-05-18 浏览次数:20977 次
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyArrayList
{
class SArrayList
{
private string[] str;
public SArrayList()
{
str = null;
}
public SArrayList this[int index]
{
get // [color=#FF0000][u][b]这个地方在输出的时候一直不停运行,怎么回事[/b][/u][/color]
{
return this[index];
}
set
{
this[index] = (SArrayList)value;
}
}
public int Length
{
get
{
if (str == null)
return 0;
else
return str.Length;
}
}
public void Add(string s)
{
string[] temp = new string[Length + 1];
for (int i = 0; i < Length; ++i)
temp[i] = str[i];
temp[Length] = s;
str = temp;
}
public int Find(string s)
{
if (Length == 0)
{
Console.Write("数组是空的!");
return -1;
}
for (int i = 0; i < Length; ++i)
if (str[i] == s)
return i;
return -1;
}
public void Remove(string s)
{
if (Find(s) == -1)
return;
int index = Find(s);
string[] temp = new string[Length - 1];
for (int i = 0; i < Length; i++)
{
if (i == index)
continue;
else if (i > index)
temp[i - 1] = str[i];
else
temp[i] = str[i];
}
str = temp;
}
public void Clear()
{
for (int i = 0; i < Length; ++i)
Remove(str[i]);
}
}
}
测试代码:
namespace MyArrayList
{
class Program
{
static void Main(string[] args)
{
SArrayList sal = new SArrayList();
for (int i = 0; i < 5; ++i)
{
sal.Add(i.ToString());
}
for (int i = 0; i < 5; ++i)
Console.Write(sal[i]+"\t");
}
}
}