C# Hashtable 正序和逆序排序
2014-05-22 16:58
246 查看
4000
本文固定链接:http://www.verydemo.com/demo_c92_i266864.html
注意:Hashtable 按键的正序排列键/值对,默认情况下的遍历GetValues是逆序遍历。
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace HashtableSort
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("+++++++++++对HashTable排序测试+++++++++++");
HashTable htb = new HashTable();
htb.Add(1, "1");
htb.Add(3, "3");
htb.Add(0, "0");
htb.Add(2, "2");
htb.Add(7, "7");
htb.Add(6, "6");
htb.Add(8, "8");
htb.Add(10, "10");
htb.Add(9, "9");
htb.Add(11, "11");
htb.GetValues();
htb.GetInfo();
htb.AscSort();
htb.DescSort();
}
}
class HashTable
{
Hashtable htb;
public HashTable()
{
htb = new Hashtable();
}
//添加键/值对
public void Add(object key, object value)
{
htb.Add(key, value);
Console.WriteLine(string.Format("向Hashtable中添加了/t键{0},值{1}", key, value));
}
//遍历Hsahtbale
public void GetValues()
{
Console.WriteLine("+++++++++++遍历Hsahtbale开始+++++++++++");
foreach(DictionaryEntry de in htb)
{
Console.WriteLine("当前Hashtable中的键/值对:/t键:{0},值:{1}",de.Key,de.Value);
}
Console.WriteLine("+++++++++++遍历Hsahtbale结束+++++++++++");
}
//得到Hashtable信息
public void GetInfo()
{
Console.WriteLine("Hashtable中有键/值对{0}对", htb.Count);
}
//对HashTable排序
public void AscSort()
{
Console.WriteLine("+++++++++++对HashTable正序排序开始+++++++++++");
ArrayList arrList = new ArrayList(htb.Keys);
arrList.Sort();
//for (int i = 0; i < arrList.Count; i++)
//{
// Console.WriteLine("/t键{0}", arrList[i]);
//}
for (int i = 0; i < arrList.Count; i++)
{
Console.WriteLine(string.Format("/t键:{0}的值是:{1}", arrList[i], htb[arrList[i]]));
}
Console.WriteLine("+++++++++++对HashTable正序排序结束+++++++++++");
}
public void DescSort()
{
Console.WriteLine("+++++++++++对HashTable逆序排序开始+++++++++++");
ArrayList arrList = new ArrayList(htb.Keys);
arrList.Sort();
//for (int i = 0; i < arrList.Count; i++)
//{
// Console.WriteLine("/t键{0}", arrList[i]);
//}
for (int i = arrList.Count-1; i >= 0; i--)
{
Console.WriteLine(string.Format("/t键:{0}的值是:{1}", arrList[i], htb[arrList[i]]));
}
Console.WriteLine("+++++++++++对HashTable逆序排序结束+++++++++++");
}
}
}
有特点的几处:arrList[i], htb[arrList[i]]
相关文章推荐
- C# Hashtable 正序和逆序排序
- Collection -> Array的排序(正序/逆序)
- Collection -> Array的排序(正序/逆序)
- Collection -> Array的排序(正序/逆序)
- map正序、逆序排序
- Collection -> Array的排序(正序/逆序)
- c语言实现直接插入排序(正序和逆序)
- C#数组的排序(正序逆序)
- 前台JQuery绑定和后台绑定 区分 +JS单引号和双引号调用函数区分+ 正序,逆序排序想法
- 【JavaScript 学习--02】--排序问题(正序和逆序)
- 通用版2.J - 归并排序求逆序对
- 并规排序求逆序对数 hud 4911
- 算法导论2.4 合并排序求逆序数
- 51Nod 1874 字符串排序 逆序对
- 正序逆序生成单链表,正序逆序输出链表(递归法和顺序栈法)
- 数组中的逆序对:归并排序的变形。
- SDUT3402数据结构实验之排序五:归并求逆序数
- 数据结构实验之排序五:归并求逆序数
- OC数组的正序倒序排序(数组元素为字符串)
- rqnoj 173 Fish学数学 求逆序数,并归排序的应用