学习集合与常用类:
1 /**
2 * 一: 设计一个程序,创建 ArrayList 对象,存储利用 Random 类产生的10个随机数值,
3 * 并在排序之后输出到控制台。
4 */
5 using System;
6 using System.Collections;
7 using System.Collections.Generic;
8 using System.Globalization;
9 using System.Linq;
10 using System.Text;
11 using System.Threading.Tasks;
12
13 namespace SecondAssignment
14 {
15 class Program
16 {
17 //排序数组
18 public void SortArrayList()
19 {
20 //创建 ArrayList 对象
21 ArrayList list = new ArrayList();
22 Random random = new Random();
23 int resultNum ;
24 for (int i = 0; i < 10; i++)
25 {
26 resultNum = random.Next(100);
27 list.Insert(i,resultNum);
28 }
29 list.Sort();
30 foreach (int item in list)
31 {
32 Console.WriteLine(item);
33 }
34 }
35 static void Main(string[] args)
36 {
37 Program p = new Program();
38 p.SortArrayList();
39 Console.ReadKey();
40 }
41 }
42 }
1 /**
2 * 二: 使用 ArrayList 类代替数组,做门票系统。
3 * 1>、当age<14,“儿童票”。
4 * 当14<=age<65,“成人票”。
5 * 当65<=age,“老年票”。
6 * 2>、打印出姓名和对应的票。
7 */
8 using System;
9 using System.Collections;
10 using System.Collections.Generic;
11 using System.Globalization;
12 using System.Linq;
13 using System.Text;
14 using System.Threading.Tasks;
15
16 namespace SecondAssignment
17 {
18 class Program
19 {
20 //门票系统
21 public void TicketsSystem()
22 {
23 //初始化数组列表
24 ArrayList namelist = new ArrayList() { "张国荣", "张 强", "小 雪" };
25 ArrayList ageList = new ArrayList() { 70, 50, 8 };
26
27 for (int i = 0; i < ageList.Count; i++)
28 {
29 int age = Convert.ToInt32(ageList[i]);
30 if (age < 14)
31 {
32 Console.WriteLine("{0} 是儿童票;", namelist[i]);
33 }
34 else if (age >= 14 && age < 65)
35 {
36 Console.WriteLine("{0} 是成人票;", namelist[i]);
37 }
38 else
39 {
40 Console.WriteLine("{0} 是老年票;", namelist[i]);
41 }
42 }
43 }
44 static void Main(string[] args)
45 {
46 Program p = new Program();
47 p.TicketsSystem();
48 Console.ReadKey();
49 }
50 }
51 }
1 /**
2 * 三: 有一个已经排好序的数组 {12,20,45,56,72,89,91,121,256}。
3 * 现输入一个数,要求按原来的规律将它插入数组中,且输出,
4 * 使用 ArrayList 类。
5 *
6 */
7
8 using System;
9 using System.Collections;
10 using System.Collections.Generic;
11 using System.Linq;
12 using System.Text;
13 using System.Threading.Tasks;
14
15 namespace SecondAssignment
16 {
17 class Class1
18 {
19 //使用 ArrayList 类.输入一个数,要求按原来的规律将它插入数组中
20 static void Main(string[] args)
21 {
22 Console.WriteLine("请输入一个数,系统将把他插入ArrayList 数组中并排序输出:");
23 int[] myArray = { 12, 20, 45, 56, 72, 89, 91, 121, 256 };
24 ArrayList newList = new ArrayList();
25 for (int i = 0; i < myArray.Length; i++)
26 {
27 newList.Insert(i, myArray[i]);
28 }
29 //Console.ReadLine()默认输出的类型为string,所以必须强转为int类型才可以排序
30 newList.Add(Convert.ToInt32(Console.ReadLine()));
31 newList.Sort();
32 Console.WriteLine("排序输出结果为:");
33 foreach (var item in newList)
34 {
35 Console.WriteLine(item);
36 }
37 Console.ReadKey();
38 }
39 }
40 }