zoukankan      html  css  js  c++  java
  • 数据结构和算法 – 6.构建字典: DictionaryBase 类和 SortedList 类

     

    6.1.DictionaryBase 类的基础方法和属性

    大家可以把字典数据结构看成是一种计算机化的词典。要查找的词就是关键字,而词的定义就是值。
    DictionaryBase 类是一种用作专有字典实现基础的抽象( MusInherit)类。
    存储在字典中的键值对实际上是作为 DictionaryEntry 对象来存储的。 DictionaryEntry 结构提供了两个域,一个用于关键字而另一个用于值。在这个结构中所要关注的只是 Key 属性和 Value 属性这两个属性(或方法)。当把键值对录入到字典内的时候,这些方法会返回存储的值。本章稍后会讨论 DictionaryEntry 对象。就内部而言,会把键值对存储在被称为 InnerHashTable 的散列表对象中。DictionaryBase 类实际上实现了来自 System.Collections 名字空间的界面,即 IDictionary。

    class DictionaryBase_类和_SortedList_类
        {
            static void Main()
            {
                IPAddresses myIPs = new IPAddresses();
                myIPs.Add("Mike", "192.155.12.1");
                myIPs.Add("David", "192.155.12.2");
                myIPs.Add("Bernica", "192.155.12.3");
                Console.WriteLine("There are " + myIPs.Count + " IP addresses");
                Console.WriteLine("David's ip address: " + myIPs.Item("David"));
                myIPs.Clear();
                Console.WriteLine("There are " + myIPs.Count + " IP addresses");
                Console.Read();
            }
    
            
        }
    
        public class IPAddresses : DictionaryBase
        {
            public IPAddresses()
            { }
            public void Add(string name, string ip)
            {
                base.InnerHashtable.Add(name, ip);
            }
            public string Item(string name)
            {
                return base.InnerHashtable[name].ToString();
            }
            public void Remove(string name)
            {
                base.InnerHashtable.Remove(name);
            }
        }

    image

     

    6.1.1.CopyTo 方法和 GetEnumerator 方法

    DictionaryEntry 结构提供了两个域,一个用于关键字而另一个用于值。在这个结构中所要关注的只是 Key 属性和 Value 属性这两个属性(或方法)。

    DictionaryEntry[] ips = new DictionaryEntry[myIPs.Count];
                myIPs.CopyTo(ips, 0);
                for (int i = 0; i <= ips.GetUpperBound(0); i++)
                {
                    Console.WriteLine(string.Format("key:{0},Value:{1}",ips[i].Key,ips[i].Value));
                }

    image

     

    6.2.泛型的 KeyValuePair 类

    一个 KeyValuePair 对象可以向下列这样实例化:

    KeyValuePair<string, int> mcmillan = new KeyValuePair<string, int>("McMillan", 99);

    这里会分别取回关键字和值:
    Console.Write(mcmillan.Key);
    Console.Write(" " + mcmillan.Value);
    KeyValuePair<string,int>[] gradeBook=new KeyValuePair<string,int>[10];
                gradeBook[0] = new KeyValuePair<string, int>("McMillan", 99);
                gradeBook[1] = new KeyValuePair<string, int>("Ruff", 64);
                for (int i = 0; i <= gradeBook.GetUpperBound(0); i++)
                    if (gradeBook[i].Value != 0)
                        Console.WriteLine(gradeBook[i].Key + ": " + gradeBook[i].Value);
                Console.Read();

    image

     

    6.3.SortedList 类

    SortedList 是按照分类顺序基于键值来存储键值对。当存储的关键字很重要时可以使用这种数据结构。比如,在标准词典中希望所存储的词是按照字母的顺序存储的情况。本章稍后还将说明如何用类来保存一个单独分类的值表。

    例如,可以把 myips 象下面这样实例化:

    SortedList<string, string> myips = new SortedList<string, string>();
                myips.Add("Mike", "192.155.12.1");
                myips.Add("David", "192.155.12.2");
                myips.Add("Bernica", "192.155.12.3");
                foreach (string key in myips.Keys)
                    Console.WriteLine("Name: " + key + "
    " + "IP: " + myips[key]);

    image

  • 相关阅读:
    Javascript(js)分页程序
    Erlang gb_trees
    ASP.NET MVC 扩展之 JsonpResult ,用来提供轻量级跨域调用服务
    facebook folly编译脚本
    开源项目 XDD
    ObjectiveC——消息、Category和Protocol
    Lua编译执行与错误
    ASP.NET MVC 4 RC的JS/CSS打包压缩功能
    web插件化
    精益企业应用平台
  • 原文地址:https://www.cnblogs.com/tangge/p/5520131.html
Copyright © 2011-2022 走看看