1
using
System;
2
3 namespace 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
2
3 namespace 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]
如果判断,在算法的复杂度就会增加一个级别.
我应该怎么办啊????