<1>两个(ArrayList)集合{ “a”,“b”,“c”,“d”,“e”}和{ “d”, “e”, “f”, “g”, “h” },把这两个集合去除重复项合并成一个。
练习01string[] strs1 = { "a", "b", "c", "d", "e" }; string[] strs2 = { "d", "e", "f", "g", "h" }; ArrayList list1 = new ArrayList(); ArrayList list2 = new ArrayList(); list1.AddRange(strs1); list2.AddRange(strs2); for (int i = 0; i < list2.Count; i++) { if (!list1.Contains(list2[i])) { list1.Add(list2[i]); } } Console.ReadKey();
<2>随机生成10个1-100之间的数放到ArrayList中,要求这10个数不能重复,并且都是偶数(添加10次,可能循环很多次。)
练习02namespace 练习题02 { class Program { static void Main(string[] args) { ArrayList list = new ArrayList(); Random r = new Random(); while (list.Count < 10) { int temp = r.Next(1, 101); if (!list.Contains(temp) && temp%2==0) { list.Add(temp); } } } } }
<3>有一个字符串是用空格分隔的一系列整数,写一个程序把其中的整数做如下重新排列打印出来:奇数显示在左侧、偶数显示在右侧。
比如”2 7 8 3 22 9 5 11”显示成”7 3 9 2 8 22….”。
练习03namespace 练习03 { class Program { static void Main(string[] args) { string str = "2 7 8 3 22 9 5 11"; string[] arr = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); //对字符串进行分离处理 ArrayList listeven = new ArrayList(); ArrayList listodd = new ArrayList(); for (int i = 0; i < arr.Length; i++) { int num = Convert.ToInt32(arr[i]); if (num % 2 == 0) { listeven.Add(arr[i]); //把偶数放在偶数集合里 } else { listodd.Add(arr[i]); //把奇数放在奇数集合里 } } listodd.AddRange(listeven); //奇数在前将偶数集合添加到奇数集合里 string[] strs = (string[])listodd.ToArray(typeof(string)); //将集合listodd转化为string 数组 } } }<4>输入一串字符串,输出每一个字符,并显示字符出现的次数
练习04namespace 练习05 { class Program { static void Main(string[] args) { Dictionary<string, int> dir = new Dictionary<string, int>(); string str = "我爱北京们都是好天安门,天安们都是好门天安门,天安们都是好门天安门,天安门上们都是好太阳升; 我们都是好孩子"; for (int i = 0; i < str.Length; i++) { string temp = str[i].ToString(); if (dir.ContainsKey(temp)) { dir[temp]++; } else { dir.Add(temp, 1); } } StringBuilder sb = new StringBuilder(); foreach (KeyValuePair<string, int> item in dir) { sb.AppendFormat("{0} \t {1}\n",item.Key,item.Value); } Console.WriteLine(sb); Console.ReadKey(); } } }
<5> 山寨版评论提交 :定义Label1显示评论时出现的字和出现的次数,Label2为显示还可以输入多少字(上限140),textBox输入文本
Button提交,字数超出上限时不能提交。
练习05namespace _06山寨微博提交客户端 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Hashtable table = new Hashtable(); private void textBox1_TextChanged(object sender, EventArgs e) { table.Clear(); // 1、检查个数 string str = txt.Text.Trim(); int num = str.Length; // int numTotal = Convert.ToInt32(total.Text); int numTotal = 140; int dif = numTotal - num; // 2、统计个数 for (int i = 0; i < str.Length; i++) { // 看看这个字符串在集合中是否存在,如果存在个数加1, // 不存在,将其加进去,将个数初始化为1 string current = str[i].ToString(); if (table.ContainsKey(current)) { // 存在 int numTemp = Convert.ToInt32(table[current]); table[current] = numTemp + 1; } else { // 不存在 table.Add(current, 1); } } // 3、显示统计结果与总数 StringBuilder sb = new StringBuilder(); foreach (DictionaryEntry item in table) { sb.AppendFormat("汉字{0}, 出现{1}次\r\n", item.Key, item.Value); } viewRes.Text = sb.ToString(); // 4、考虑按钮是否可用 if (dif < 0) { btnSender.Enabled = false; } else { btnSender.Enabled = true; } total.Text = dif.ToString(); } private void Form1_Load(object sender, EventArgs e) { } } }