动态数组设计原理
/// <summary>
/// ObjArray 的摘要说明。
/// 动态数组
/// </summary>
/// <example>
/// ObjectArray DynArr = new ObjectArray();
/// DynArr.addItem(11);
/// DynArr.addItem("12");
/// DynArr.getItem(i);
/// DynArr.clearAll();
/// </example>
public class ObjArray
{
//对象数组
private object[] Items;
//对象数组的长度
public int Length
{
get
{
if(Items == null)
return 0;
else
return Items.Length;
}
}
/// <summary>
/// 构造函数
/// </summary>
public ObjArray()
{
//
// TODO: 在此处添加构造函数逻辑
//
Items = null;
}
/// <summary>
/// 添加一元素
/// </summary>
/// <param name="item">元素</param>
public void addItem(object item)
{
if(Items == null)
{
Items = new object[1];
Items[0] = item;
return;
}
else
{
object[] tmpList = new object[Length+1];
for(int i=0;i<Length;i++)
{
tmpList[i] = Items[i];
}
tmpList[Length] = item;
Items = tmpList;
return;
}
}
/// <summary>
/// 删除一个元素
/// </summary>
/// <param name="index">元素索引</param>
public void deleteItem(int index)
{
if(Items == null)
return;
else
{
//如果索引没有越界
if(index >=0 && index < Length && Length>1)
{
object[] tmpList = new object[Length-1];
for(int i=0;i<index;i++)
{
tmpList[i] = Items[i];
}
for(int i=index+1;i<Length;i++)
{
tmpList[i-1] = Items[i];
}
Items = tmpList;
return;
}
else
{
if(index == 0 && Length<=1)
{
Items = null;
return;
}
else
return;
}
}
}
/// <summary>
/// 读取元素
/// </summary>
/// <param name="index">元素索引</param>
/// <returns>一个元素</returns>
public object getItem(int index)
{
if(Items == null)
return null;
else
{
if(index >= 0 && index < Length)
return Items[index];
else
return null;
}
}
/// <summary>
/// 清空数组
/// </summary>
public void clearAll()
{
Items = null;
}
}