自己写的一个字符索引器,有关于效率和代价问题请教各位

今天早上醒来精神不错,就写了一个字符索引器, 希望各位指出不足之处,无论是编程习惯还是缺陷!
  1 None.gif using  System;
  2 None.gif
  3 None.gif namespace  Company
  4 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  5ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
  6InBlock.gif    /// Class1 的摘要说明。
  7ExpandedSubBlockEnd.gif    /// </summary>

  8InBlock.gif    public class Company
  9ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 10InBlock.gif        public string   Name; 
 11InBlock.gif        private Employee[] emp;
 12InBlock.gif        System.Collections.ArrayList indexList=new System.Collections.ArrayList();//存储字符索引的数组
 13InBlock.gif        private static int   i=0;
 14InBlock.gif
 15InBlock.gif        public Company(string n)
 16ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 17InBlock.gif            this.Name = n;
 18InBlock.gif            emp=new Employee[100];
 19ExpandedSubBlockEnd.gif        }
 
 20InBlock.gif        public Employee this [int index]
 21ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 22InBlock.gif            get
 23ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 24InBlock.gif                if(indexList.Count<index)//整型索引超出边界
 25ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 26InBlock.gif                    //throw Exception;
 27InBlock.gif                    return null;
 28ExpandedSubBlockEnd.gif                }

 29InBlock.gif                else
 30InBlock.gif                    return emp[index];
 31ExpandedSubBlockEnd.gif            }

 32InBlock.gif            set
 33ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 34InBlock.gif                if (value != null)
 35ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 36InBlock.gif                    emp [index] = value;
 37ExpandedSubBlockEnd.gif                }

 38ExpandedSubBlockEnd.gif            }

 39ExpandedSubBlockEnd.gif        }
//indexer
 40InBlock.gif        public Employee this [string index]
 41ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 42InBlock.gif            get
 43ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 44InBlock.gif                return emp[GetIndex(index)];
 45ExpandedSubBlockEnd.gif            }

 46InBlock.gif            set
 47ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 48InBlock.gif                if (value != null)//Company[dot.gif]的值不为空
 49ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 50InBlock.gif                    if(indexList.Count==0)
 51ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 52InBlock.gif                        indexList.Add(index);
 53InBlock.gif                        i++;
 54InBlock.gif                        emp[GetIndex(index)] = value;
 55ExpandedSubBlockEnd.gif                    }

 56InBlock.gif                    else
 57ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 58InBlock.gif                        for(int j=0;j< indexList.Count;j++)
 59ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{                                    
 60InBlock.gif                            if(index!=(string)indexList[j])//判断字符索引是否重复
 61ExpandedSubBlockStart.gifContractedSubBlock.gif                            dot.gif{
 62InBlock.gif                                indexList.Add(index);//相索引列表中添加索引值
 63InBlock.gif                                i++;
 64InBlock.gif                                emp [GetIndex(index)] = value;
 65ExpandedSubBlockEnd.gif                            }

 66InBlock.gif                            else
 67ExpandedSubBlockStart.gifContractedSubBlock.gif                            dot.gif{
 68InBlock.gif                                emp [GetIndex(index)] = value;//如果重复则覆盖原来的实例
 69ExpandedSubBlockEnd.gif                            }

 70ExpandedSubBlockEnd.gif                        }

 71ExpandedSubBlockEnd.gif                    }

 72ExpandedSubBlockEnd.gif                }

 73ExpandedSubBlockEnd.gif            }

 74ExpandedSubBlockEnd.gif        }
//indexer
 75InBlock.gif        private int GetIndex(string index)//根据字符索引返回整型索引
 76ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 77InBlock.gif            int i=0;
 78InBlock.gif            for(int j=0;j<indexList.Count;j++)
 79ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 80InBlock.gif                if(index==(string)indexList[j])
 81ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 82InBlock.gif                    return i;
 83ExpandedSubBlockEnd.gif                }

 84InBlock.gif                i++;
 85ExpandedSubBlockEnd.gif            }

 86InBlock.gif            return -1;
 87ExpandedSubBlockEnd.gif        }

 88InBlock.gif        
 89ExpandedSubBlockEnd.gif    }

 90InBlock.gif    public class Employee
 91ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 92InBlock.gif        private string name;
 93InBlock.gif        public Employee(string n)
 94ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 95InBlock.gif            this.name=n;
 96ExpandedSubBlockEnd.gif        }

 97InBlock.gif        public string Name
 98ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 99InBlock.gif            get
100ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
101InBlock.gif                return this.name;
102ExpandedSubBlockEnd.gif            }

103ExpandedSubBlockEnd.gif        }

104ExpandedSubBlockEnd.gif    }

105InBlock.gif    public class test
106ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
107InBlock.gif        static void Main()
108ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
109InBlock.gif            Company c=new Company("company");
110InBlock.gif            c["employe"]=new Employee("emp1");
111InBlock.gif            c["employe"]=new Employee("emp2");
112InBlock.gif            Console.WriteLine(c[0].Name);
113ExpandedSubBlockEnd.gif        }

114ExpandedSubBlockEnd.gif    }

115InBlock.gif
116ExpandedBlockEnd.gif}

117 None.gif

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


转载于:https://www.cnblogs.com/Aldebaran/archive/2006/04/23/382711.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值