zoukankan      html  css  js  c++  java
  • 集合>哈希表类Hashtable和SortedList排序列表类

    集合>哈希表类Hashtable

    Hashtable一种键值对的集合 ,哈希表内部的排列是无序的,而且哈希表没有提供排序方法。

    集合>哈希表类Hashtable>构造普通哈希表

    代码
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;

    namespace ConsoleApplication1
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                
    //使用所有默认值构建哈希表实例
                Hashtable ht = new Hashtable();
                
    //指定哈希表实例的初始容量为20个元素
                Hashtable ht1 = new Hashtable(20);
                
    //指定初始容量为20个元素,加载因子为0.8的哈希表实例,加载因子大,哈希表自动扩展容量也大。
                Hashtable ht2 = new Hashtable(200.8f);
                
    //实例化一个SortedList。
                SortedList sl = new SortedList();
                sl.Add(
    "键一""键值一");
                sl.Add(
    "键二""键值二");
                sl.Add(
    "键三""键值三");
                
    //传入实现了IDictionary接口的参数创建哈希表。
                Hashtable ht3 = new Hashtable(sl);
            }
        }
    }


    集合>哈希表类Hashtable>获取哈希表值

    代码
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;

    namespace useHashtable
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                
    //初始化哈希表
                Hashtable hl = new Hashtable();
                
    //使用Add方法向哈希表添加元素。
                hl.Add("键一""键值一");
                hl.Add(
    "键二""键值二");
                hl.Add(
    "键三""键值三");
                hl.Add(
    "键四""键值四");
                hl.Add(
    "键五""键值五");
                hl.Add(
    "键六""键值六");
                hl.Add(
    "键七""键值七");
                DisplayResult(hl);
                Console.WriteLine(
    "通过键获取键值:"+ hl["键一"]);
                Console.WriteLine(
    "移除哈希表中的元素");
                hl.Remove(
    "键二");
                Console.WriteLine(
    "哈希表中的元数总数是:" + hl.Count);
                DisplayResult(hl);
                Console.ReadLine();
            }
            
    static void DisplayResult(Hashtable tl)
            {
                
    foreach (DictionaryEntry de in tl)
                { 
                    Console.WriteLine(
    "哈希表中的键:{0},对应的值{1}",de.Key,de.Value);
                }
            }
        }
    }


    集合>哈希表类Hashtable>哈希表排序

    代码
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;

    namespace HashSort
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                Hashtable ht 
    = new Hashtable();
                ht.Add(
    "key1""this is value1");
                ht.Add(
    "key3""this is vlaue3");
                ht.Add(
    "key2""this is value2");
                ht.Add(
    "key4""this is value4");
                Console.WriteLine(
    "排序前的哈希键值顺序为:");
                
    foreach (DictionaryEntry de in ht)
                { 
                    Console.WriteLine(
    "键{0},值{1}", de.Key, de.Value);
                }
                ICollection keys 
    = ht.Keys;
                ArrayList al 
    = new ArrayList(keys);
                al.Sort();
                Console.WriteLine(
    "排序后的哈希键值顺序为:");
                
    foreach (object key in al)
                {
                    Console.WriteLine(
    "键{0},值{1}", key, ht[key]);
                }
                Console.ReadLine();
            }
        }
    }


    集合>哈希表类Hashtable>哈希表搜索

    代码
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;

    namespace HashtableSearch
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                Hashtable ht 
    = new Hashtable();
                ht.Add(
    "key1""this is value1");
                ht.Add(
    "key3""this is vlaue3");
                ht.Add(
    "key2""this is value2");
                ht.Add(
    "key4""this is value4");          
                ICollection keys 
    = ht.Keys;
                ArrayList al 
    = new ArrayList(keys);
                al.Sort();
                
    //在这里使用ArrayList的BinarySearch搜索指定键值的值。
                int searchResult = al.BinarySearch("key3");
                
    if (searchResult > 0)
                {
                    Console.WriteLine(
    "搜索到的结果为:键{0},值{1}",al[searchResult],ht[al[searchResult]]);
                }
                
    else
                {
                    Console.WriteLine(
    "没有搜索到有效的结果值");
                }
                Console.ReadLine();
            }
        }
    }


    集合>SortedList排序列表类

    SortedList对象是可以排序的字典对象。与哈希表不同的是,SortedList内部的键是一个数组,值也是一个数组。这两个数组有初始容量,可自动扩充,SortedList根据键来排序,所键要实现IComparable接口。

    集合>SortedList排序列表类>创建和操作

    代码
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;

    namespace ConsoleApplication1
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                
    //默认值构建
                SortedList sl=new SortedList();
                sl.Add(
    "键1""键值一");
                sl.Add(
    "键2""键值二");
                sl.Add(
    "键3""键值三");
                sl.Add(
    "键4""键值四");
                sl.Add(
    "键5""键值六");
                sl.Add(
    "键6""键值七");
                Console.WriteLine(
    "排序字典的最初的初始化列表为:");
                DisplayResult(sl);
                sl.Remove(
    "键5");
                Console.WriteLine(
    "键6的键值是:{0}", sl["键6"]);            
                Console.ReadLine();
            }
            
    static void DisplayResult(SortedList sl)
            {
                
    foreach (DictionaryEntry de in sl)
                {
                    Console.WriteLine(
    "键:{0},键值为:{1}", de.Key, de.Value);
                }
            }
        }
    }


     


    合乎自然而生生不息。。。
  • 相关阅读:
    IIS 浏览aspx页面出现无法显示XML页的解决方法分享
    好看的导航菜单
    C# FileUpload使用过程遇到的问题
    javascript之数组操作
    浅谈.NET下的多线程
    项目分页通用页
    npoi
    react webpack.config.js的配置及注释
    JS 解析Json 方法
    json解析
  • 原文地址:https://www.cnblogs.com/samwu/p/1848163.html
Copyright © 2011-2022 走看看