9-11上课笔记
(1)随机数种子的问题:
Rando r=new Random();//通过Random产生的随机数是伪随机数,伪随机数需要一个种子
int n=r.Next();
Console.writeline(n);
Console.ReadKey();
ArrayList
Hashtable
(2)泛型集合
List<T>
Dictionary<K,V>
方法:
add() remove() RemoveAt()
sort() max() min() sum() avg()
T,K,V就像是锁,锁住集合中只能存放某种特点的类型
泛型集合可以进行foreach遍历,是因为他们都实现了IEnumerable
static void Main(string[]args)
{
//通过泛型可以限制集合中存放的数据的类型
List<int>list=new List<int>();///int类型的
list.Add(10);
list.Add(20);
list.Add(30);
List<string>list=new List<string>()//string类型的
list.Add("hello word");
list.Add("hello");
Console.writeLine(list.Max());
Console.writeLine(list.Min());
Console.writeLine(list.Sum());
Console.writeLine(list.Average());
for(int i=0;i<list.Count;i++)
{
Console.Writeline(list[i]);
}
}
//返回值就是整型数组
//int[] lists= list.ToArray();
(3)字典 dictionary
static void Main(string[]args)
{
Dictionary<string,string>dict=new Dictionary<string,string>();
dict.Add("yqq","杨巧巧");
dict.Add("lxf","刘晓飞");
dict.Add("zl","张兰");
dic.Add("ly",刘祎);
if(!dict.ContainsKey("ly"))
{
dict.Add("ly","刘祎");
}
}
Console.writeline(dict.containskey("zl"));
console.writeline(dict["zl"]);
//遍历值
foreach(string item in dict.Values)
{
Console.WriteLine(item);
}
//遍历键
foreach(string item dict.Key)
{
Console.WriteLine(item);
}
//键和值同时遍历
(1)方法:
foreach (DictionaryEntry item in table)
{
Console.WriteLine(item.Key + " " + item.Value);
}
Console.ReadKey();
(2)方法:
foreach (KeyValuePair<string,Person>kv in dict)
{
Console.Writeline("键:{0},值:{1}",kv,Person.value);
}
例2
Dictionary<string,Person>dict=new Dictionary<string,Person>()
Person p1=new Person(){Name="zl",Age=18};
dict.Add(p1.Name,p1);
console.writeline((dict[p1.Name]).Name+"的年龄是:"+dict[p1.Name].Age);
//定义一个Person类
class Person
{
public string Name
{
get;
set;
}
}
public int Age
{
get;
set;
}
练习题:
2.讲int数组中的奇数放到一个新的int数组中返回
int[] number={2,7,9,4,3,8,1,6};
List<int>listOdd=new List<int>();
for(int i=0;i<number.Length;i++)
{
if(number[i]%2!=0)
listOdd.Add(number[i]);
}
int[]numOdd=listOdd.ToArray();
3.从整数的List<int>中找最大值,不适用Max方法。
List<int> list=new List<int>(1,2,3,4,5,6,7,8,9);
//Random radom=new Random();
//radom.next()
//list.Add
int temp=0
for(int i=0;i<list.Count;i++)
{
if(list[i]>temp)
{
temp=list[i];
}
}
console.writeline("max is :"+temp);
//4.计算字符串中每种字符出现的次数。(面试题)“welcome to china! this //is a beautiful county, i think you will like it.here is The great wall//”提示:Dictionary<char,int>
string str = "Welcome to china! this is a beautiful county, i think you will like it.here is The great wall";
str = str.ToLower();
Dictionary<char, int> dict2 = new Dictionary<char, int>();
for (int i = 0; i < str.Length; i++)
{
if (char.IsLetter(str[i]))//是不是字母
{
if (dict2.ContainsKey(str[i]))
{
dict2[str[i]]++;
}
else
{
dict2.Add(str[i], 1);
}
}
}
foreach (KeyValuePair<char, int> kv in dict2)
{
Console.WriteLine("字符:{0},出现了{1}次",kv.Key,kv.Value);
}
Console.ReadKey();
5.把1,2,3转换为壹 贰 叁
static void Main(string[] args)
{
Console.WriteLine("请输入一个数:");
string number=Console.ReadLine();
//-------------------------------------------
string str = "1壹 2贰 3叁 4肆 5伍 6陆 7柒 8捌 9玖 0零";
Dictionary<char, char> dict = new Dictionary<char, char>();
string[] parts = str.Split(' ');
for (int i = 0; i < parts.Length; i++)//把数组中的值放到字典中
{
dict.Add(parts[i][0], parts[i][1]);
}
//----------------------------------根据输入的数把键值找出来
StringBuilder sb=new StringBuilder();
for (int i = 0; i < number.Length; i++)
{
sb.Append(dict[number[i]]);//789存到一个字符串里 得到7对应的值 返回的是大写的柒;
}
Console.WriteLine(sb.ToString());
Console.ReadKey();
}
你