您的位置:首页 > 其它

继承List<T>类,并且完成List的Add()(在集合的末尾添加)方法和Get()(通过索引的方式获取)方法。并且写出方法的时间复杂度。

2013-04-13 07:37 676 查看
思路分析:因为题目要求在末尾添加,通过索引的方式获取所以使用数组存储集合中的对象。使用数组的方式存储对象,Get方法通过下标访问的时间复杂度为o(1),如何使得Add方法添加的时间复杂度最低呢?记得以前学习C#时,在List中有一个属性Capacity,这个是指集合中的容量,并且每次集合容量满后,都会将容量扩展到当前集合的两倍,这时时间复杂度为o(2ln(n))。

实现MyList<T>类。

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace Interview
7 {
8     /// <summary>
9     /// 自定义的集合类
10     /// (1)以数组的方式实现T对象对策存储,存储在t_arr数组中
11     /// (2) 容器中的存储容量。同时每一次容器中容量不够时,
12     /// (3)向容器中增加到当前容量两倍大小的容量,这样可以使得添加操作的时间复杂度尽可能的接近o(1)
13     /// </summary>
14     /// <typeparam name="T"></typeparam>
15     class MyList<T> : List<T>
16     {
17
18         private T[] t_arr;
19
20         private int capacity = 10;
21         /// <summary>
22         /// 集合的容量。默认值初始化值为10。
23         /// </summary>
24         public int Capacity
25         {
26             set { this.capacity = value; }
27             get { return this.capacity; }
28         }
29
30         private int length = 0;
31         /// <summary>
32         /// 集合实际的存储的对象个数
33         /// </summary>
34         public int Length
35         {
36             set { this.length = value; }
37             get { return this.length; }
38         }
39
40         /// <summary>
41         /// MyList构造方法。
42         /// </summary>
43         public MyList(){
44             t_arr = new T[this.capacity];
45         }
46
47         /// <summary>
48         /// MyList构造方法。它允许用户设定初始容量
49         /// </summary>
50         /// <param name="capacity">初始容量的大小</param>
51         public MyList(int capacity)
52         {
53             this.capacity = capacity;
54             t_arr = new T[this.capacity];
55         }
56
57
58         /// <summary>
59         /// 添加一个对象。在集合的末尾添加一个T类的对象
60         /// </summary>
61         /// <param name="t">被添加的对象</param>
62         public void Add(T t)
63         {
64             //如果当前容量不够,则将容量扩大打当前容量的双倍
65             if (this.length == this.capacity)
66             {
67                 this.capacity  = 2 * capacity;
68                 T[] temp = new T[this.capacity];
69                 //将原来t_arr数组的数据复制到新的数组中。
70                 for (int i = 0; i < this.t_arr.Length; i++)
71                 {
72                     temp[i] = this.t_arr[i];
73                 }
74                 t_arr = temp;
75             }
76             //添加对象到集合中
77             this.t_arr[this.length] = t;
78             this.length++;
79         }
80
81         /// <summary>
82         /// 根据索引获取对象
83         /// </summary>
84         /// <param name="index">试图访问的元素的下标索引</param>
85         /// <returns>返回一个T对象</returns>
86         public T Get (int index)
87         {
88             if (index > this.length - 1|| index < 0)
89             {
90                 throw new IndexOutOfRangeException("试图访问的索引超出了集合的范围");
91             }
92
93             return this.t_arr[index];
94         }
95     }
96 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐