继承List<T>类,并且完成List的Add()(在集合的末尾添加)方法和Get()(通过索引的方式获取)方法。并且写出方法的时间复杂度。
2013-04-13 07:37
676 查看
思路分析:因为题目要求在末尾添加,通过索引的方式获取所以使用数组存储集合中的对象。使用数组的方式存储对象,Get方法通过下标访问的时间复杂度为o(1),如何使得Add方法添加的时间复杂度最低呢?记得以前学习C#时,在List中有一个属性Capacity,这个是指集合中的容量,并且每次集合容量满后,都会将容量扩展到当前集合的两倍,这时时间复杂度为o(2ln(n))。
实现MyList<T>类。
实现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 }
相关文章推荐
- java中通过反射获取方法并且调用(getMethod和invoke深入)实践
- Java 集合之List 集合的添加方法顺序分析以及add和addAll区别
- Java中集合list的add方法添加的不是值COPY而是地址
- List.add方法——向集合列表中添加对象
- Java中集合list的add方法添加的是地址(引用)不是值
- 原生js实现增加(addclass),删除(removeclass),判断是否存在(hasclass),如果存在删除,如果不存在添加(toggleclass)和获取类名(getbyclass)的方法
- java获取URL参数(get方式,并且你不知道他末尾有什么参数名)
- list不通过add方法添加元素,用什么方法
- Java中集合list的add方法添加的不是值COPY而是地址
- java中通过反射获取方法并且调用(getMethod和invoke深入)实践
- 实现一个栈(元素遵守先入后出顺序),能够通过 min 方法在 O(1)时间内获取栈中的最小元素。同时,栈的基本操作:入栈(Push)、出栈(Pop),也是在O(1)时间内完成的
- 在service层,通过get方法获取到map集合中的值报错
- java中通过反射获取方法并且调用(getMethod和invoke深入)实践
- list的add方法 ,foreach循环添加map---List.add(map)(通过一个java爬虫案例说明)
- Spring中c3p0连接池的配置 及JdbcTemplate的使用 通过XML配置文件注入各种需要对象的操作 来完成数据库添加Add()方法
- jquery的get方式的ajax方法带参数的那种会出现获取到的参数是乱码的解决
- List 中添加多个List集合以及add() 与addAll()的区别
- Div使用name属性实现通过getElementsByName获取Div集的方法
- Android 最简单的获取系统时间并且格式化的方法
- 带Checkbox的TreeView(一) 完美兼容IE、Firefox ,在js中添加了getCurrentNode(evt)方法,注册方法变为TreeView1.Attributes.Add("onclick", "CheckEvent