---------------------- <a href="http://edu.youkuaiyun.com"target="blank">ASP.Net+Android+IO开发S</a>、<a href="http://edu.youkuaiyun.com"target="blank">.Net培训</a>、期待与您交流! ----------------------
其实最近的学习遇到了一些瓶颈,在不断的学习中接触到更多的概念性的东西和更为复杂些的东西,其实说复杂,只是我们不了解它,没有接触过,不知道它有什么好处,用在哪里,比如泛型类,泛型方法,集合,List类(一些相关的ArrayList类,Dictionary类),类的多态,及接口的实现,接口中显式实现和隐式实现,还有System.IO中文件的处理,还有Hashtable类(哈希列表),一下子接触到这么多,真怕自己不理解,影响后面的学习,便去搜索,去理解,去查MSDN,经过一番的查阅,老实说头晕的要死,有一些简单的用法到能学会,但是他们的具体的还是很模糊,卡在这里会很难受,有时会怀疑自己的能力,怕自己学不会.看到博客论坛中很多的东西都看不懂,觉得别人好像都比自己强,我也有这种感觉,尤其这几天.经过几天的思考,我觉得我不应该怀疑自己的,其实每个人的学习过程都是一样的,是循序渐进的过程,我现在充分的体会到了这一点,别人牛,有几种可能,就是别人接触得早,学习的时间比我们长,也许要长得多,还有就是我们把自己想象得太好了,以为自己的能力特别强,所以一下子不理解了,就失落了.还好我想通了,我现在只需要再继续学习,不太理解的暂时简单的会用就好,把基础打好,慢慢的去理解吸收,在一个休息的时间,精神很好的时候,再拿来研究和巩固.
今天我看到了Dictionary类的用法,我突然想到之前基础测试的最后一题,有难到我,我想了最笨的办法,后来我又学习了foreach
今天就重新用新方法,到网上借鉴了下,写了下面的代码
我之前写的,先全部转成大写,最后用24个英文字母分别和字符串中的每个字符比较,得到出现的次数,最笨的方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace text10
{
class Program
{
static void Main(string[] args)
{
//第十题、计算字符串中每种字符出现的次数。“Welcome to Chinaworld”,不区分大小写,打印“W 2”“e 2”“l 3”……
//定义要判断的数组
string arr = "welcome to chinaworld";
//将数组中的字母全部转成大写
string Arr = arr.ToUpper();
char[] Ar = new char[arr.Length];
for (int i = 0; i < arr.Length; i++)
{
Ar[i] = Arr[i];
}
//定义了一个包含24个英文字母的数组
string letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char[] let = new char[24];
for (int i = 0; i < 24; i++)
{
let[i] = letter[i];
}
//判断字符串中每个英文字母的出现次数
int[] temp = new int[24];
for (int i = 0; i < 24; i++)
{
for (int j = 0; j < arr.Length; j++)
{
if (let[i] == Ar[j])
temp[i] += 1;
}
}
//最后输出如下
for (int i = 0; i < 24; i++)
{
if (temp[i] != 0)
Console.Write("{0}{1} ", let[i], temp[i]);
}
Console.ReadKey();
}
}
}
现在我用到了字典集合,只是知道了字符串名,就可以检索字符串.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace txet10的扩展
{
class Program
{
static void Main(string[] args)
{
//第十题、计算字符串中每种字符出现的次数。“Welcome to Chinaworld”,
// 不区分大小写,打印“W 2”“e 2”“l 3”……
string str = "Welcome to Chinaworld";
//new一个字典集合
Dictionary<char, int> dic = new Dictionary<char, int>();
for (int i = 0; i < str.Length; i++)
{
if(str[i]==' ')
{
continue;
}
if (dic.ContainsKey(str[i]) == true)//这的字符串名str便指明了要检索哪个字符串
{
//以字符串第一个字符开始检索,出现第二次叠加1,以此类推
dic[str[i]]++;
}
else
{
//只出现第一次,value的值为1
dic[str[i]] = 1;
}
}
foreach (KeyValuePair<char, int> kv in dic)
{
//把每个键,及出现的次数(value)的值依次打印出来
Console.WriteLine("字母{0},出现了{1}次", kv.Key, kv.Value);
}
Console.ReadKey();
}
}
}
另外以前遇到的一题判断String 2: DCGSRQPOMZ 是否所有的字符都在String
1: ABCDEFGHLMNOPQRS 中出现过.
用哈希列表算的话,非常的简单,我刚学哈希,简单实现了下,循环次数只用了16+10次
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace text11扩展新题
{
class Program
{
static void Main(string[] args)
{
string str1 = "ABCDEFGHLMNOPQRS";
string str2 = "DCGSRQPOMZ";
//new下Hashtable的新实例
Hashtable ht = new Hashtable();
//将str1中的每个字符放到hashtable中
foreach (char s in str1)
{
ht.Add(s, s);
}
//通过以str2中每个字符为键,比较是否出现过
Boolean bo=true;
foreach (char s in str2)
{
bo= ht.ContainsKey(s);
}
//最后的str2中的Z,没有出现过结果为false
Console.WriteLine(bo);
Console.ReadKey();
}
}
}
其实这些新知识只是初步的了解,简单的实现了下,容易出现错误,以后是要再加深理解的