ArrayList

 1     class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             //创建了一个集合对象
 6             ArrayList list = new ArrayList();
 7             //集合:很多数据的一个集合
 8             //数组:长度不可变、类型单一
 9             //集合的好处:长度可以任意改变  类型随便
10             list.Add(1);
11             list.Add(3.14);
12             list.Add(true);
13             list.Add("张三");
14             list.Add('');
15             list.Add(5000m);
16             list.Add(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
17             Person p = new Person();
18             list.Add(p);
19             list.Add(list);
20             //list.AddRange(new string[]{})
21             for (int i = 0; i < list.Count; i++)
22             {
23                 if (list[i] is Person)
24                 {
25                     ((Person)list[i]).SayHello();
26                 }
27                 else if (list[i] is int[])
28                 {
29                     for (int j = 0; j < ((int[])list[i]).Length; j++)
30                     {
31                         Console.WriteLine(((int[])list[i])[j]);
32                     }
33                 }
34                 else
35                 {
36                     Console.WriteLine(list[i]);
37                 }
38 
39 
40                 //Console.WriteLine(list[i]);
41             }
42             Console.ReadKey();
43         }
44     }
45 
46     public class Person
47     {
48         public void SayHello()
49         {
50             Console.WriteLine("我是人类");
51         }
52     }
ArrayList集合
 1     class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             ArrayList list = new ArrayList();
 6             //添加单个元素
 7             list.Add(true);
 8             list.Add(1);
 9             list.Add("张三");
10             //添加集合元素
11             list.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
12             //list.AddRange(list);
13 
14             //list.Clear();清空所有元素
15             //list.Remove(true);删除单个元素 写谁就删谁
16             //list.RemoveAt(0);根据下标去删除元素
17             //list.RemoveRange(0, 3);根据下标去移除一定范围的元素
18             // list.Sort();//升序排列
19             //list.Reverse();反转
20             //list.Insert(1, "插入的");在指定的位置插入一个元素
21             //list.InsertRange(0, new string[] { "张三", "李四" });在指定的位置插入一个集合
22             //bool b = list.Contains(1);判断是否包含某个指定的元素
23             list.Add("颜世伟");
24             if (!list.Contains("颜世伟"))
25             {
26                 list.Add("颜世伟");
27             }
28             else
29             {
30                 Console.WriteLine("已经有这个屌丝啦");
31             }
32             for (int i = 0; i < list.Count; i++)
33             {
34                 Console.WriteLine(list[i]);
35             }
36             Console.ReadKey();
37         }
38     }
ArrayList的各种方法
 1     class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             ArrayList list = new ArrayList();
 6             list.Add(1);
 7             list.Add(1);
 8             list.Add(1);
 9             list.Add(1);
10             list.Add(1);
11             list.Add(1);
12             list.Add(1);
13             list.Add(1);
14             list.Add(1);
15             Console.WriteLine(list.Count);
16             Console.WriteLine(list.Capacity);
17             Console.ReadKey();
18             //count 表示这个集合中实际包含的元素的个数
19             //capcity 表示这个集合中可以包含的元素的个数
20         }
21     }
ArrayList集合长度的问题
 1         static void Main(string[] args)
 2         {
 3             //创建一个集合,里面添加一些数字,求平均值与和,最大值,最小值
 4             //ArrayList list = new ArrayList();
 5             //list.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
 6             //int sum = 0;
 7             //int max = (int)list[0];
 8             //for (int i = 0; i < list.Count; i++)
 9             //{
10             //    if ((int)list[i] > max)
11             //    {
12             //        max = (int)list[i];
13             //    }
14             //    sum += (int)list[i];
15             //}
16             //Console.WriteLine(sum);
17             //Console.WriteLine(max);
18             //Console.WriteLine(sum/list.Count);
19             //Console.ReadKey();
20             //写一个长度为10的集合,要求在里面随机地存放10个数字(0-9),
21             //但是要求所有的数字不重复
22             //ArrayList list = new ArrayList();
23             //Random r = new Random();
24             //for (int i = 0; i <10; i++)
25             //{
26             //    int rNumber = r.Next(0, 10);
27             //    //集合中没有这个随机数
28             //    if (!list.Contains(rNumber))
29             //    {
30             //        list.Add(rNumber);
31             //    }
32             //    else//集合中有这个随机数
33             //    {
34             //        //一旦产生了重复的随机数 这次循环就不算数
35             //        i--;
36             //    }
37 
38             //}
39 
40 
41             //for (int i = 0; i < list.Count; i++)
42             //{
43             //    Console.WriteLine(list[i]);
44             //}
45             //Console.ReadKey();
46 
47 
48 
49             string str = "2++b/c*d/e";
50             string[] strNew = str.Split(new char[] { '+', '-', '*', '/' });
51 
52 
53 
54             StringBuilder sb = new StringBuilder();//capcity count
55             
56             sb.Append("12312312312312312");
57             Console.WriteLine(sb.Capacity);
58             //char[] chs=  {'1','2','3'};
59             Console.ReadKey();
60 
61         }
集合的练习

 

posted @ 2020-09-21 16:38  技术不够脸来凑  阅读(112)  评论(0)    收藏  举报