C#,入门教程(35)——哈希表(Hashtable)的基础知识与用法

上一篇:

C#,入门教程(34)------关于函数的参数之引用(ref)的一点知识与源程序https://blog.csdn.net/beijinghorn/article/details/125411351

有一段故事:

King Log

The frogs in the lake had an easy life doing exactly what they wanted. But what pleased one frog annoyed another, and they could see things could be better. All the frogs agreed they needed a strong leader to make the rules they should live by. So they sent a message to the King of all animals. "Very well," said the King, and threw a log into a lake, telling the frogs this was their new leader.

At first the frogs were terrified of it.

When it splashed into the lake they all dived to the bottom and hid in mud. But after a while, when the log did nothing but float on the surface, they lost their fear. They hopped all over it and carried on as before. They sent another message to the King saying they needed a better one.

"Then you must learn your lesson," said the King. This time he sent a water snake, who took one look at all the frogs and ate as many of them as it could catch.

问题是:

(1)这段文字中有 lesson 这个词吗?

(2)frogs 这个词出现了几次?

(3)frogs 这个词在哪些位置出现?

用程序解答以上问题,最方便与快速的,莫过于 哈希表 Hashtable 了。

哈希表Hashtable是一种常用数据集合。

仔细读一读下面的代码,你基本上可以知道怎么用了。

cs 复制代码
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;


// 1、将故事文字组成一个字符串
StringBuilder sb = new StringBuilder();
sb.AppendLine("King Log");
sb.AppendLine("The frogs in the lake had an easy life doing exactly what they wanted. But what pleased one frog annoyed another, and they could see things could be better. All the frogs agreed they needed a strong leader to make the rules they should live by. So they sent a message to the King of all animals. \"Very well,\" said the King, and threw a log into a lake, telling the frogs this was their new leader.");
sb.AppendLine("At first the frogs were terrified of it.");
sb.AppendLine("When it splashed into the lake they all dived to the bottom and hid in mud. But after a while, when the log did nothing but float on the surface, they lost their fear. They hopped all over it and carried on as before. They sent another message to the King saying they needed a better one.");
sb.AppendLine("\"Then you must learn your lesson,\" said the King. This time he sent a water snake, who took one look at all the frogs and ate as many of them as it could catch.");

// 2、字符串按空格,分隔成字(表)
string[] words = sb.ToString().Split(' ');

// 3、创建一个保存字、位置信息的哈希表
Hashtable hash = new Hashtable();

// 4、字的相对位置
int offset = 0;
foreach (string word in words)
{
	// 如果哈希表中,尚未保存 word 信息
	if (!hash.ContainsKey(word))
	{
		// 添加一个字、位置(列表)的信息;
		hash.Add(word, new List<int>() { offset });
	}
	else
	{
		// 获取已经保存的位置列表信息,加入新的位置
		List<int> list = (List<int>)hash[word];
		list.Add(offset);
	}
	// 位置偏移量往后增加 word 的长度,和1空格;
	offset += word.Length + 1;
}

// 清除 StringBuilder 的原有信息,后面用于显示计算结果
sb.Clear();
// 回答题目中的三个问题
sb.AppendLine("1. Has word 'lesson' ? " + hash.ContainsKey("lesson") + "<br>");
List<int> fc= (List<int>)hash["frogs"];
sb.AppendLine("2. Word 'frogs' appears " + fc.Count + " times.<br>");
sb.AppendLine("3. Word 'frogs' appears at " + String.Join(",", fc.ToArray()) + " <br>");
// 答案显示于 webBrowser1 组件。
webBrowser1.DocumentText = sb.ToString();

代码不长,作为初学者,如果能把每句话都读懂,就能进一大步。

作业:

怎么能搜索某个词在第几行(或那几行)出现?


POWER BY 315SOFT.COM &
TRUFFER.CN

下一篇:

C#,入门教程(36)------尝试(try)捕捉(catch)不同异常(Exception)的点滴知识与源代码https://blog.csdn.net/beijinghorn/article/details/124271911

相关推荐
盛夏绽放41 分钟前
Python字符串常用方法详解
开发语言·python·c#
Tummer83634 小时前
C#+WPF+prism+materialdesign创建工具主界面框架
开发语言·c#·wpf
ghost1435 小时前
C#学习第23天:面向对象设计模式
开发语言·学习·设计模式·c#
yngsqq6 小时前
(for 循环) VS (LINQ) 性能比拼 ——c#
c#·solr·linq
想做后端的小C7 小时前
C# 面向对象 构造函数带参无参细节解析
开发语言·c#·面向对象
炯哈哈7 小时前
【上位机——WPF】App.xml和Application类简介
xml·开发语言·c#·wpf·上位机
bestcxx7 小时前
c# UTC 时间赋值注意事项
c#·utc
酷炫码神7 小时前
C#运算符
开发语言·c#
zybsjn8 小时前
后端系统做国际化改造,生成多语言包
java·python·c#
敲代码的 蜡笔小新9 小时前
【行为型之迭代器模式】游戏开发实战——Unity高效集合遍历与场景管理的架构精髓
unity·设计模式·c#·迭代器模式