数组与集合

数组分类:一维数组 多维数组 不规则数组。

数组的应用:创建 赋值 应用
Array:所有数组的基类。
Rank:获取数组维度。
Reverse(Array):逆转整个一维数组中元素的值。
Params[]:参数数组。(可变参数)。

交叉数组是数组的数组

Array类提供了各种数组的属性和方法

在c#中您可以传递数组作为函数的参数

在使用数组作为形参时C#提供了params关键字,调用数组作为形参的方法时既可以传递数组实参,也可以值传递一组数组。

BCL中集合类型分为泛型集合和非泛型集合


集合用于存储类型不固定,长度可动态添加元素的容器
动态数组(ArrayList)它代表了可被单独索引的对象的有序集合
.哈希表(Hashtable)他是用键来访问集合中的元素
.排序列表(SortedList)用于处理和表现类似key value的键值对,它可以使用键和索引来访问列表中的项
.堆栈(Stack)约等于栈 它代表了一个后进先出(先进后出)的对象集合
队列(QUeue)约等于堆 他代表了一个先进先出的对象及合
System.Collection 命名空间的类
(1)动态数组(ArrayList):动态数组(ArrayList) 它代表了可被单独索引的对象的有序集合。它基本上可以替代一个数组。但是,与数组不同的是,您可以使用索引在指定的位置添加和移除项目,动态数组会自动重新调整它的大小。它也允许在列表中进行动态内存分配、增加、搜索、排序各项。
(2)哈希表(Hashtable):它使用键来访问集合中的元素。当您使用键访问元素时,则使用哈希表,而且您可以识别一个有用的键值。哈希表中的每一项都有一个键/值对。键用于访问集合中的项目。
(3) 排序列表(SortedList):用于处理和表现类似key value的键值对,它可以使用键和索引来访问列表中的项。排序列表是数组和哈希表的组合。它包含一个可使用键或索引访问各项的列表。如果您使用索引访问各项,则它是一个动态数组(ArrayList),如果您使用键访问各项,则它是一个哈希表(Hashtable)。集合中的各项总是按键值排序。
(4)堆栈(Stack):它代表了一个后进先出的对象集合。当您需要对各项进行后进先出的访问时,则使用堆栈。当您在列表中添加一项,称为推入元素,当您从列表中移除一项时,称为弹出元素。
(5)队列(Queue):它代表了一个先进先出的对象集合。当您需要对各项进行先进先出的访问时,则使用队列。当您在列表中添加一项,称为入队,当您从列表中移除一项时,称为出队。

声明一个数组不会再内存中创建数组

 

 static void Main(string[] args)
        {
            #region 一维数组
            //数组的第一种定义方式
            string[] values = { "123", "22", "2123", "333" };

            //数组的第二种定义方式
            int[] nums = new int[3];
            //数组赋值
            for (int i = 0; i < nums.Length; i++)
            {
                nums[i] = i;
            }
            #region 数组赋值
            //nums[0] = 1;
            //nums[1] = 2;
            //nums[2] = 3;
            //nums[3] = 4; 
            #endregion
            //nums = new int[4]; 
            #endregion

            #region 二维数组
            //二维数组定义
            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();
            //}
            #endregion

            #region 三维数组
            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];
            }
            #endregion

            #region 交错数组
            int[][] scores = new int[3][];
            scores[0] = new int[] { 1, 2, 3 };
            scores[1] = new int[] { 1, 2, 3 };
            scores[2] = new int[] { 1, 2, 3 };
            //Console.WriteLine(scores.Length);
            //for (int i = 0; i < scores.Length; i++)
            //{
            //    int[] arry = scores[i];
            //    for (int j = 0; j < arry.Length; j++)
            //    {
            //        Console.Write(arry[j]+"   ");
            //    }
            //    Console.WriteLine();
            //}
            #endregion

            #region Array
            Array array = new int[2,3];

            #endregion

            #region 静态方法和非静态方法的调用
            //MyClass.GetName("我是鹏");
            //MyClass myClass = new MyClass();
            //myClass.GetValue("my name is qinyonghui");
            ////声明string数组
            //string[] strs = new string[array.Rank];
            //for (int i = 0; i < array.Rank; i++)
            //{
            //    strs[i] = "鹏" + i;
            //}
            //myClass.GetArray(strs);
            //myClass.GetArray(new int[] { 1,2,3});
            //myClass.GetArrayStr((string[])array);
            #endregion

            #region 参数数组
            //MyClass myClass = new MyClass();
            //myClass.Print();
            //myClass.Print("鹏");
            //myClass.Print("秋", "飞");
            //myClass.Print("跃", "熹", "静");
            //string[] names = new string[3] { "跃", 晨熹", "静" };
            //myClass.Print(names); 
            #endregion

            #region ArrayList
            ArrayList arrayList = new ArrayList();
            arrayList.Add(new string[] { "122", "222" });
            arrayList.Add(1);
            arrayList.Add(1.2);
            arrayList.Add(null);
            arrayList.Add(new MyClass());
            MyClass myClass = new MyClass();
            //myClass.
            arrayList.RemoveRange(1, 1);
            arrayList.RemoveAt(0);
            arrayList.Remove(1);
            int count = arrayList.Count;
            //Console.WriteLine(count);
            //foreach (var item in arrayList)
            //{
            //    if (item!=null)
            //    {
            //        Console.WriteLine(item.ToString()); 
            //    }
            //}
            //Console.WriteLine();
            #endregion

            #region HashTable
            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;
            //Console.WriteLine("删除一个元素后的长度:" + count1);

            //Console.WriteLine("清除集合后的长度:" + count2);
            #endregion

            #region 堆栈
            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);
            #endregion

            #region 队列
            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();
        }

  

原文地址:https://www.cnblogs.com/nxj1997/p/11142217.html