关于泛型的作业题(跪求大神帮忙)

a1302534151 2016-06-06 02:08:00
(1)添加一个继承自List<T>的类ListEX<T>,假设T类型不提供对象比较的方法,为该类添加方法CountAll(),输入参数为T类型对象,该方法用于计算指定参数出现在ListEX<T>中的次数;假设T类型实现了接口IComparable,提供了Compare()方法,可以对两个T类型的对象进行大小的比较,修改CountAll()方法,先将泛型中的数据元素排序,再计算指定参数出现在ListEX<T>中的次数。
(2)在主函数中创建该泛型类的实例,并为该类添加10个元素,1、2、2、3、3、3、4、4、4和4;分别调用(2)中的两个CountAll()方法计算1、2、3和4在集合中出现的次数,并输出。
(3)试着在(2)中的两个CountAll()方法中添加计时功能,再次运行程序,体会排序是否提高了程序的时间效率。
...全文
211 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
南天空 2016-06-06
  • 打赏
  • 举报
回复
using System; using System.Collections.Generic; public class Temperature : IComparable<Temperature> { // Implement the generic CompareTo method with the Temperature // class as the Type parameter. // public int CompareTo(Temperature other) { // If other is not a valid object reference, this instance is greater. if (other == null) return 1; // The temperature comparison depends on the comparison of // the underlying Double values. return m_value.CompareTo(other.m_value); } // The underlying temperature value. protected double m_value = 0.0; public double Celsius { get { return m_value - 273.15; } } public double Kelvin { get { return m_value; } set { if (value < 0.0) { throw new ArgumentException("Temperature cannot be less than absolute zero."); } else { m_value = value; } } } public Temperature(double kelvins) { this.Kelvin = kelvins; } } public class Example { public static void Main() { SortedList<Temperature, string> temps = new SortedList<Temperature, string>(); // Add entries to the sorted list, out of order. temps.Add(new Temperature(2017.15), "Boiling point of Lead"); temps.Add(new Temperature(0), "Absolute zero"); temps.Add(new Temperature(273.15), "Freezing point of water"); temps.Add(new Temperature(5100.15), "Boiling point of Carbon"); temps.Add(new Temperature(373.15), "Boiling point of water"); temps.Add(new Temperature(600.65), "Melting point of Lead"); foreach( KeyValuePair<Temperature, string> kvp in temps ) { Console.WriteLine("{0} is {1} degrees Celsius.", kvp.Value, kvp.Key.Celsius); } } } 楼主参考这个例子,基本上换下类型就能出来。
  • 打赏
  • 举报
回复
这个作业你自己写了多少了?哪里有问题?
为轮子而生 2016-06-06
  • 打赏
  • 举报
回复
要写全部代码的话工作量略大

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧