今天早上醒来精神不错,就写了一个字符索引器,希望各位指出不足之处,无论是编程习惯还是缺陷!
  using System;namespace Company
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class Company
{
public string   Name; 
private Employee[] emp;
System.Collections.ArrayList indexList=new System.Collections.ArrayList();
private static int   i=0; public Company(string n)
{
this.Name = n;
emp=new Employee[100];

public Employee this [int index]
{
get
{
if(indexList.Count<index)
{
//throw Exception;
return null;
}
else
return emp[index];
}
set
{
if (value != null)
{
emp [index] = value;
}
}
}//indexer
public Employee this [string index]
{
get
{
return emp[GetIndex(index)];
}
set
{
if (value != null)//bc[...]的值不为空
{
if(indexList.Count==0)
{
indexList.Add(index);
i++;
emp[GetIndex(index)] = value;
}
else
{
for(int j=0;j< indexList.Count;j++)
{
if(index!=(string)indexList[j])//判断字符索引是否重复
{
indexList.Add(index);//相索引列表中添加索引值
i++;
emp [GetIndex(index)] = value;
}
else
{
emp [GetIndex(index)] = value;//如果重复则覆盖原来的实例
}
}
}
}
}
}//indexer
private int GetIndex(string index)
{
int i=0;
for(int j=0;j<indexList.Count;j++)
{
if(index==(string)indexList[j])
{
return i;
}
i++;
}
return -1;
}

}
// public class test
// {
// static void Main()
// {
// Company c=new Company("company");
// c["employe"]=new Employee("emp1");
// c["employe"]=new Employee("emp2");
// Console.WriteLine(c[0].Name);
// }
// }}
这里有个问题,在实际应用中,如果出现了
c["employe"]=new Employee("emp1");
c["employe"]=new Employee("emp2");
对于c["employe"]应该是覆盖,还是直接indexList.Add(index);(第48行)
到底应不应该判断索引标签是否重复的那段?
如果不判断,indexList(ArrayList类型)就会庞大,出现c[0]=c[1]
如果判断,在算法的复杂度就会增加一个级别.
我应该怎么办啊????