·RSS订阅本站
Dev-club通告
·Dev-Club开始提供RSS订阅
·Dev-Club博客开通,现在开通即可获得50兆文件上传空间
泛型入门学习十:终结篇_泛型C#示例  >>

mingziweb 于 2006-4-24 10:01:10 加贴在 .NET技术 ←返回版面按此给该网友发送邮件 按此察看该网友的资料 按此把文章加入书签 按此给作者留言 按此给作者发送即时消息 按此查看作者个人专辑 按此打印本帖 按此打包转发本帖

// 版权所有 (C) Microsoft Corporation。保留所有权利。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Generics_CSharp
{
    // 尖括号中的类型参数 T。
    public class MyList<T> : IEnumerable<T>
    {
        protected Node head;
        protected Node current = null;

        // 嵌套类型也是 T 上的泛型
        protected class Node
        {
            public Node next;
            // T 作为私有成员数据类型。
            private T data;
            // 在非泛型构造函数中使用的 T。
            public Node(T t)
            {
                next = null;
                data = t;
            }
            public Node Next
            {
                get { return next; }
                set { next = value; }
            }
            // T 作为属性的返回类型。
            public T Data
            {
                get { return data; }
                set { data = value; }
            }
        }

        public MyList()
        {
            head = null;
        }

        // T 作为方法参数类型。
        public void AddHead(T t)
        {
            Node n = new Node(t);
            n.Next = head;
            head = n;
        }

        // 实现 GetEnumerator 以返回 IEnumerator<T>,从而启用列表的
        // foreach 迭代。请注意,在 C# 2.0 中,
        // 不需要实现 Current 和 MoveNext。
        // 编译器将创建实现 IEnumerator<T> 的类。
        public IEnumerator<T> GetEnumerator()
        {
            Node current = head;

            while (current != null)
            {
                yield return current.Data;
                current = current.Next;
            }
        }

        // 必须实现此方法,因为
        // IEnumerable<T> 继承 IEnumerable
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }


    public class SortedList<T> : MyList<T> where T : IComparable<T>
    {
        // 一个未优化的简单排序算法,
        // 该算法从低到高对列表元素排序:
        public void BubbleSort()
        {
            if (null == head || null == head.Next)
                return;

            bool swapped;
            do
            {
                Node previous = null;
                Node current = head;
                swapped = false;

                while (current.next != null)
                {
                    // 由于需要调用此方法,因此,SortedList
                    // 类在 IEnumerable<T> 上是受约束的
                    if (current.Data.CompareTo(current.next.Data) > 0)
                    {
                        Node tmp = current.next;
                        current.next = current.next.next;
                        tmp.next = current;

                        if (previous == null)
                        {
                            head = tmp;
                        }
                        else
                        {
                            previous.next = tmp;
                        }
                        previous = tmp;
                        swapped = true;
                    }

                    else
                    {
                        previous = current;
                        current = current.next;
                    }

                }// 结束 while
            } while (swapped);
        }
    }

    // 一个将自身作为类型参数来实现 IComparable<T> 的简单类,
    // 是对象中的
    // 常用设计模式,这些对象
    // 存储在泛型列表中。
    public class Person : IComparable<Person>
    {
        string name;
        int age;

        public Person(string s, int i)
        {
            name = s;
            age = i;
        }

        // 这会使列表元素
        // 按 age 值排序。
        public int CompareTo(Person p)
        {
            return age - p.age;
        }

        public override string ToString()
        {
            return name + ":" + age;
        }

        // 必须实现 Equals。
        public bool Equals(Person p)
        {
            return (this.age == p.age);
        }
    }

    class Generics
    {
        static void Main(string[] args)
        {
            // 声明并实例化一个新的范型 SortedList 类。
            // Person 是类型参数。
            SortedList<Person> list = new SortedList<Person>();

            // 创建 name 和 age 值以初始化 Person 对象。
            string[] names = new string[] { "Franscoise", "Bill", "Li", "Sandra", "Gunnar", "Alok", "Hiroyuki", "Maria", "Alessandro", "Raul" };
            int[] ages = new int[] { 45, 19, 28, 23, 18, 9, 108, 72, 30, 35 };

            // 填充列表。
            for (int x = 0; x < names.Length; x++)
            {
                list.AddHead(new Person(names[x], ages[x]));
            }

            Console.WriteLine("Unsorted List:");
            // 打印出未排序的列表。
            foreach (Person p in list)
            {
                Console.WriteLine(p.ToString());
            }

            // 对列表进行排序。
            list.BubbleSort();

            Console.WriteLine(String.Format("{0}Sorted List:", Environment.NewLine));
            // 打印出排序的列表。
            foreach (Person p in list)
            {
                Console.WriteLine(p.ToString());
            }

            Console.WriteLine("Done");
        }
    }

}


计算机世界开发者俱乐部 http://dev-club.esnai.com

你觉得本精华贴如何? 请给本精华贴打分

  0 1 2 3 4 5
一塌糊涂   妙不可言
* 游客不能打分。
打分结果:
总分0 0次
0 1 2 3 4 5
0人给本贴打过分

相关帖子  >>
回复这个精华帖
版面: .NET技术  ←返回版面  >>


你还没有登录! 第一次发言请去注册
* 用户名: * 密码: 下次自动登录 密码遗忘?
* 校验码: 请在验证码框输入
  Email: 回复请Email通知 如果不填写则取注册Email
* 主题: (还可以输入100字节) 不能正常发言?  
    * 如果你是要向他人求助,请把主题类型设置为“求助” 如何在社区提问?  怎么在帖子里面发布音乐、视频信息?  
  表情:  
   
   
  内容(最多16KB): 签名 设置  发言注意事项↓ ALT+S发送
   

  上传附件:  
    允许的文件类型:JPG,GIF,ZIP,RAR,最大不能超过2048KB)
图片缩小的工具,如果您上传的JPEG图片比较大,建议您使用这个工具作尺寸调整
 
  链接地址:  
  链接标题:  
  链接图片:  

注意: 在计算机世界开发者俱乐部发言之前您必须仔细阅读并同意下列条款:
·尊重网上道德,遵守《全国人大常委会关于维护互联网安全的决定》及中华人民共和国其他各项有关法律法规
·严禁发表危害国家安全、破坏民族团结、破坏国家宗教政策、破坏社会稳定、侮辱、诽谤、教唆、淫秽等内容的作品
·承担一切因您的行为而直接或间接导致的民事或刑事法律责任
·计算机世界开发者俱乐部各栏目的版主有权保留或删除其管辖论坛中的任意内容
·您在计算机世界开发者俱乐部发表的作品,计算机世界开发者俱乐部有权在网站内免费转载或引用
发言注意事项:
·带有*号的内容为必填内容
·只有本版版主或积分大于50的人才可以帖图哦!
·怎样使用ABC代码

Dev-club通告
·Dev-Club开始提供RSS订阅
·Dev-Club博客开通,现在开通即可获得50兆文件上传空间