《C#高级特性》 数组 集合

1、数组概念:数组是一个容器。

2、特点:长度可变,类型相同。

3、变量是程序运行时在内存中存储可变数据的容器,可以存储单个数据。

数组的分类:一维数组,多维数组,不规则数组

数组的应用:创建,赋值,应用。

例如:

      //数组的第一种定义方式
            string[] values = { "123", "22", "2123", "333" };

            //数组的第二种定义方式
            int[] nums = new int[3];
            //数组赋值
            for (int i = 0; i < nums.Length; i++)
            {
                nums[i] = i;
            }
            二维数组定义
            int[,] intArry = new int[3, 3] { { 2, 3, 3 }, { 3, 4, 5 }, { 5, 6, 7 } };
            intArry = new int[2, 3];
            int nums1 = intArry.GetLength(1);

            Console.WriteLine(nums1);
            for (int i = 0; i < intArry.Length; i++)
            {
                Console.WriteLine("222");
            }
            for (int i = 0; i < intArry.GetLength(0); i++)
            {
                for (int j = 0; j < intArry.GetLength(1); j++)
                {
                    Console.Write(intArry[i, j] + "  ");
                }
                Console.WriteLine();
            }
//定义三维数组    
int[,,] numsss = new int[2, 3, 4] { 
                {
                    { 1, 2, 3, 4 },
                    { 5, 6, 7, 8 }, 
                    { 9, 10, 11, 12 }
                }, 
                { 
                    { 13, 14, 15, 16 }, 
                    { 17, 18, 19, 20 }, 
                    { 21, 22, 23, 24 }
                }
            };
            int[,,,] numssss = new int[2, 3, 4, 5] {
                {
                    {
                        { 1, 2, 3, 4, 5 },
                        { 1, 2, 3, 4, 5 },
                        { 1, 2, 3, 4, 5 },
                        { 1, 2, 3, 4, 5 }
                    },
                    {
                        { 1, 2, 3, 4, 5 },
                        { 1, 2, 3, 4, 5 },
                        { 1, 2, 3, 4, 5 },
                        { 1, 2, 3, 4, 5 }
                    },
                    {
                        { 1, 2, 3, 4, 5 },
                        { 1, 2, 3, 4, 5 },
                        { 1, 2, 3, 4, 5 },
                        { 1, 2, 3, 4, 5 }
                    }
                }, {
                     {
                        { 1, 2, 3, 4, 5 },
                        { 1, 2, 3, 4, 5 },
                        { 1, 2, 3, 4, 5 },
                        { 1, 2, 3, 4, 5 }
                    },
                    {
                        { 1, 2, 3, 4, 5 },
                        { 1, 2, 3, 4, 5 },
                        { 1, 2, 3, 4, 5 },
                        { 1, 2, 3, 4, 5 }
                    },
                    {
                        { 1, 2, 3, 4, 5 },
                        { 1, 2, 3, 4, 5 },
                        { 1, 2, 3, 4, 5 },
                        { 1, 2, 3, 4, 5 }
                    }
                }
            };
            //Console.WriteLine(numssss.Length);
            for (int i = 0; i < numssss.Length; i++)
            {
                //int j = numssss[0, 0, 0, 0];
            }

集合:动态数组:代表了可被单独索引的对象的有序集合

   排序函数:用于处理和表现类似Key Value的键值对

   堆栈约等于栈:它代表了一个后进先出的对象集合

//
 Stack stack = new Stack();
            stack.Push("易先鹏");
            stack.Push("王剑飞");
            stack.Push("鲁长秋");
            //Console.WriteLine(stack.Count);
            //取堆栈的元素
            string name1 = stack.Peek().ToString();
            //Console.WriteLine(name1);
            int length = stack.Count;
            for (int i = 0; i < length; i++)
            {
                string names= stack.Pop().ToString();
                //Console.WriteLine(names); 
            }
            //Console.WriteLine(stack.Count);

      队列约等于堆:他代表一个先进先出的对象集合

//队列      
Queue queue = new Queue();
            queue.Enqueue("易先鹏");   //入列
            queue.Enqueue("王剑飞");   //入列
            queue.Enqueue("鲁长秋");   //入列
            queue.Enqueue("王金鑫");   //入列
            Console.WriteLine(queue.Count);
            int length1 = queue.Count;
            for (int i = 0; i < length1; i++)
            {
                string names = queue.Dequeue().ToString(); //出列
                Console.WriteLine(names);
            }
            Console.WriteLine("队列所剩人数:"+ queue.Count);
            #endregion
            Console.Read();

   哈希表:它使用键来访问集合中的元素

          Hashtable hashtable = new Hashtable();
            hashtable.Add("帝都", "北京");
            hashtable.Add("魔都", "上海");
            //hashtable.Add("魔都", "广州");
            string name = hashtable["帝都"].ToString();
            hashtable.Remove("魔都");
            int count1 = hashtable.Count;
            hashtable.Clear();
            int count2 = hashtable.Count;

猜你喜欢

转载自www.cnblogs.com/bly319/p/11142290.html
今日推荐