首页 > 代码库 > c#笔记(十二)——集合

c#笔记(十二)——集合

前面的类 
classMyList
    {
 
        private int [] arr = new int[10];
 
        public int this[int index]
        {
            get
            {
                if (index >= arr.Length)
                {
                    return 0;
                }
                else if (index < 0)
                {
                    return 0;
                }
                else {
                    return arr[index];
                }
            }
            set
            {
                if (index >= 0 && index < arr.Length)
                {
                    arr[index] = value;
                }
            }
        }
    }
 
    public static class PrintTool
    {
        public static void PrintStack( Stack stack)
        {
            foreach (object item in stack)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine();
            Console.WriteLine("-----------" );
        }
 
        public static void PrintQueue( Queue queue)
        {
            foreach (var item in queue)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("\n-----------" );
        }
    }
 
 
主函数里面的
//Stack 的用法:
            Stack stack = new Stack();
            //Push方法:往集合里添加一个元素,并且把这个元素放到顶部
            stack.Push(1);
            stack.Push( "lanou");
            //Person p = new Person ();
            //stack.Push (p);
            PrintTool.PrintStack(stack);
 
            //Pop方法:移除并返回集合顶部的对象
            object popItem = stack.Pop();
            Console.WriteLine("popItem:" + popItem);
            PrintTool.PrintStack(stack);
 
            //Peek方法:返回集合中顶部的对象
            object peekItem = stack.Peek();
            Console.WriteLine(peekItem);
            PrintTool.PrintStack(stack);
 
            //Contains:判断集合中是否存在这个元素
            bool cItem = stack.Contains("lanou" );
            Console.WriteLine(cItem);
 
            //Clear:删除集合中的所有元素
            stack.Clear();
            PrintTool.PrintStack(stack);
 
            //Count:返回集合中当前元素个数
            int count = stack.Count;
            Console.WriteLine(count);
 
            stack.Push(1);
 
            //Colne:
            int[] a = { 1, 2, 3, 4 };
            int[] b;
            b = ( int[])a.Clone();
            b[0] = 2;
            foreach (var item in b)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine(a.Equals(b));
 
            //Queue:队列, 先进先出
            Queue queue = new Queue();
            //Enqueue: 将一个元素放入集合的尾部
            queue.Enqueue(1);
            queue.Enqueue( "lanou");
            queue.Enqueue(1.5);
            queue.Enqueue(5.0f);
            PrintTool.PrintQueue(queue);
 
            //Dequeue: 将集合首个元素删除,并返回
            object deItem = queue.Dequeue();
            Console.WriteLine(deItem);
            PrintTool.PrintQueue(queue);
 
            //Add:往集合中添加指定的键、值元素
            Dictionary<int , string> dic = new Dictionary <int, string>();
            dic.Add(101, "张三");
            dic.Add(102, "李四");
            dic.Add(103, "李四");
 
            foreach (var item in dic)
            {
                Console.WriteLine("key:" + item.Key + "   value:" + item.Value);
            }
            Console.WriteLine("-------------" );
 
            //TryGetValue:尝试通过键去获得值,若成功则返回true,并且将键相应的值
            //通过out参数返回,否则返回false,out参数返回“”字符串
            string tryItem;
            bool tryResult = dic.TryGetValue(105, out tryItem);
            Console.WriteLine("tryItem:" + tryItem + "   tryResult:" + tryResult);
 
            foreach (var item in dic)
            {
                Console.WriteLine("key:" + item.Key + "   value:" + item.Value);
            }
            Console.WriteLine("-------------" );
            //Clear:
            //dic.Clear ();
            foreach (var item in dic)
            {
                Console.WriteLine("key:" + item.Key + "   value:" + item.Value);
            }
            Console.WriteLine("-------------" );
 
            //Remove:删集合中指定键值
            dic.Remove(102);
            foreach (var item in dic)
            {
                Console.WriteLine(item);
            }
 
            //字典取值:可以通过[key]的方式来取的对应的值
            string value = http://www.mamicode.com/dic[101];
            Console.WriteLine("101:" + value);
 
            //ContainsKey:判断集合中是否存在某个键
            bool isFindKey = dic.ContainsKey(105);
            Console.WriteLine(isFindKey);
            //ContainsValue: 判断集合中是否存在某个值
            bool isFindValue = http://www.mamicode.com/dic.ContainsValue("老王" );
            Console.WriteLine(isFindValue);
 
            //遍历集合中的所有键
            //1. 把keys放到变量中
            /*Dictionary<int ,string>.KeyCollection keys = dic.Keys;
                                                foreach (var item in keys) {
                                                                
                                                }*/
 
            foreach (var item in dic.Keys)
            {
                Console.WriteLine(item);
            }
 
            //遍历集合中的所有值
            foreach (var item in dic.Values)
            {
                Console.WriteLine(item);
            }
 
            // 用户输入10个姓名,可以重复输入同一个姓名,
            //输出每个姓名,并输出每个姓名输入的多少次。
            /*Dictionary<string ,int> student = new Dictionary<string, int> ();
                                
                                                for (int i = 0; i < 10; i++) {
                                                                //输入一个学生的名字
                                                                string name = Console.ReadLine ();
                                                                //如果该学生已经被添加到集合中,那么将他的票数加1
                                                                if (student.ContainsKey (name)) {
                                                                                student [name] += 1;
                                                                } else {//否则,加入这个学生,并且初始票数设置为1
                                                                                student.Add (name , 1);
                                                                }
                                                }
 
                                                foreach (var item in student) {
                                                                Console.WriteLine (item.Key+":"+item.Value);
                                                }*/
 
 
            //--------
            ArrayList arrayList = new ArrayList();
            arrayList.Add(1);
            arrayList.Add(2);
            arrayList.Add( ‘A‘);
            arrayList.Add( "sss");
 
            foreach (var item in arrayList)
            {
                Console.WriteLine(item);
            }
 
            //AddRange:可以将一个继承了ICollection接口的集合里面的
            //元素挨个添加进来
            //把一个字典加入的arraylist中
            arrayList.AddRange(dic);
            //
            arrayList.AddRange(queue);
 
            foreach (var item in arrayList)
            {
                Console.WriteLine(item);
            }
            //Contains:
            arrayList.Contains( ‘A‘);
 
            //可以将集合中的元素通过此方法,拷贝到一个足够长的数组中
            object[] arr = new object[arrayList.Count];
            arrayList.CopyTo(arr);
 
            Console.WriteLine("CopyTo---------------" );
            foreach (var item in arr)
            {
                Console.WriteLine(item);
            }
 
            //取值方式和一维数组一样
            Console.WriteLine(arrayList[0]);
 
            MyList mylist = new MyList();
            mylist[0] = 12;
            Console.WriteLine(mylist[0]);
 
            Console.ReadKey();

c#笔记(十二)——集合