惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Last Week in AI
Last Week in AI
D
DataBreaches.Net
腾讯CDC
Recent Announcements
Recent Announcements
有赞技术团队
有赞技术团队
A
About on SuperTechFans
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
云风的 BLOG
云风的 BLOG
罗磊的独立博客
月光博客
月光博客
MyScale Blog
MyScale Blog
U
Unit 42
Martin Fowler
Martin Fowler
Stack Overflow Blog
Stack Overflow Blog
T
Tailwind CSS Blog
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
G
Google Developers Blog
博客园 - 【当耐特】
D
Docker
I
InfoQ
雷峰网
雷峰网

博客园 - Aldebaran's Home

copy 命令复制大批文件时不覆盖现有文件 DataTable 与 Entity 间的转换工具 Levenshtein distance算法:计算两个字符串的差异 SQL 调优 IE 与 FireFox差别 ---javascript 我的常用工具 Html的一些小东西 - Aldebaran's Home - 博客园 职业发展三岔口-技术还是管理?那太难抉择了 Nhibernate学习点滴<一> 配置注意事项 - Aldebaran's Home WebPart全接触1(如何管理自己的WebPart个性化数据) 比尔·盖茨 经典语录 您的PC还处于待机中吗?让他参与科学计算吧! 关于控件的中事件的执行顺序的问题? 所有管理者最关心的问题-如何激发员工高绩效地工作? (转) 幻灯片 - Aldebaran's Home - 博客园 2007工作总结 发现一些有趣的工具 关于动态添加AsyncPostBackTrigger失效的问题 工作一个月的感想
自己写的一个字符索引器,有关于效率和代价问题请教各位
Aldebaran's Home · 2006-04-23 · via 博客园 - Aldebaran's Home

今天早上醒来精神不错,就写了一个字符索引器,希望各位指出不足之处,无论是编程习惯还是缺陷!

  1using System;
  2
  3namespace Company
  4{
  5    /// <summary>
  6    /// Class1 的摘要说明。
  7    /// </summary>

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

 29                else
 30                    return emp[index];
 31            }

 32            set
 33            {
 34                if (value != null)
 35                {
 36                    emp [index] = value;
 37                }

 38            }

 39        }
//indexer
 40        public Employee this [string index]
 41        {
 42            get
 43            {
 44                return emp[GetIndex(index)];
 45            }

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

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

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

 70                        }

 71                    }

 72                }

 73            }

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

 84                i++;
 85            }

 86            return -1;
 87        }

 88        
 89    }

 90    public class Employee
 91    {
 92        private string name;
 93        public Employee(string n)
 94        {
 95            this.name=n;
 96        }

 97        public string Name
 98        {
 99            get
100            {
101                return this.name;
102            }

103        }

104    }

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

114    }

115
116}

117


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