c# hashtable排序问题

gameboyying 2008-02-02 07:42:16
以下是我编的程序,主要功能,是可以输入一句话,然后统计这段话中每个单词出现的次数,然后出现次数少的在前,多的在后,就是说按升序排列,应该是对HashTable的VALUE排序,不过我弄不出来,以下是我的代码!我是用VS 2005进行编辑的!
using System;
using System.Collections;
namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Please enter one sentence");
String strSentence = Console.ReadLine();
String[] strArr = strSentence.Split();

Hashtable hTable = new Hashtable();
for (int i = 0; i < strArr.Length; i++)
{
String strCurrWord = strArr[i];
if (hTable.ContainsKey(strCurrWord))
{
int counter = (int)hTable[strCurrWord];
counter++;
hTable[strCurrWord] = counter;
}
else
{
hTable[strCurrWord] = 1;
}

}

ArrayList newArrayList = new ArrayList(hTable.Values);

newArrayList.Sort(); // 升序排列

ArrayList newArray = new ArrayList();

foreach (int score in newArrayList)
{

if (!newArray.Contains(score))
{
ICollection colKeys = hTable.Keys;
IEnumerator it = colKeys.GetEnumerator();

while (it.MoveNext())
{
if ((int)it.Current== score)
{

String strKey = (String)it.Current;
Console.Out.WriteLine("The word" + strKey + "has appeared" + hTable[strKey] + "times!");
}

}

newArray.Add(score);

}
}

Console.ReadLine();
}
}

}
...全文
84 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
taodm 2008-02-02
  • 打赏
  • 举报
回复
去C#版问。

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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