首页 > 代码库 > C# 数组 【温故而知新】

C# 数组 【温故而知新】

简单的数组知识代码及注释讲解

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 数组
{
    class Program
    {
        static void Main(string[] args)
        {
            //数组的声明
            int[] iArr;
            string[] sArr;

            //数组的初始化
            iArr = new int[4];
            sArr = new string[2];

            //同时声明和初始化数组
            string[] str = new string[2];

            //数组初始化器
            int[] ii = new int[7] { 3, 4, 5, 6, 7, 8, 9 };//数组初始化器只能在声明数组变量时使用,不能在声明数组之后使用
            int[] iii = new int[] { 3, 4, 5, 6, 7, 8, 9 };//用花括号初始化数组大小,可以不指定数组的大小,编译器会自动统计元素的个数
            int[] iiii = { 1, 2, 3, 4, 5, 6, 7 };//更简化的方式

            //访问数组

            //通过索引访问
            int[] myArr = new int[] { 4, 7, 11, 2 };
            int v1 = myArr[0];
            int v2 = myArr[1];
            myArr[3] = 44;//更改数组中的值

            //通过循环索引访问
            for (int i = 0; i < myArr.Length; i++)
            {
                Console.WriteLine(myArr[i]);
            }

            //通过迭代器访问
            foreach (var val in myArr)
            {
                Console.WriteLine(val);
            }

            //使用引用类型数组

            //自定义类型的数组
            Person[] myPerson = new Person[2];

            //为数组的每个元素分配内存
            myPerson[0] = new Person { FirstName = "aa", LastName = "bb" };
            myPerson[1] = new Person { FirstName = "cc", LastName = "dd" };

            //自定义类型数组使用初始化器
            Person[] myPerson2 ={new Person{FirstName = "aa", LastName = "bb"  },
                                 new Person {FirstName = "cc", LastName = "dd"}};

            //多维数组

            //数组在初始化时候指定每一维的大小(也称为阶),声明数组之后就不能修改其阶数了
            int[,] twoDim = new int[3, 3];

            twoDim[0, 0] = 1;
            twoDim[0, 1] = 1;
            twoDim[0, 2] = 1;
            twoDim[1, 0] = 1;
            twoDim[1, 1] = 1;
            twoDim[1, 2] = 1;
            twoDim[2, 0] = 1;
            twoDim[2, 1] = 1;
            twoDim[2, 2] = 1;

            //使用数组初始化器初始二维数组
            int[,] twoArr ={
                                {1,2,3},
                                {4,5,6},
                                {7,8,9}
                            };

            //使用数组初始化器初始三维数组
            int[, ,] threeArr ={
                                     {{1,2,3},{4,5,6}},
                                     {{7,8,9},{10,11,12}},
                                     {{13,14,15},{16,17,18}}
                               };

            Console.WriteLine(threeArr[1, 1, 2]);

            //锯齿数组:锯齿数组的大小设置比较灵活,每一行都可以有不同的大小

            //初始化锯齿数组
            int[][] jagged = new int[3][];
            jagged[0] = new int[2] { 1, 2 };
            jagged[1] = new int[6] { 3, 4, 5, 6, 7, 8 };
            jagged[2] = new int[3] { 9, 10, 11 };

            //迭代锯齿数组中的元素
            for (int row = 0; row < jagged.Length; row++)
            {
                for (int element = 0; element < jagged[row].Length; element++)
                {
                    Console.WriteLine("row:{0},element:{1},value:{2}", row, element, jagged[row][element]);
                }
            }

            Console.ReadKey();
        }
    }

    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public override string ToString()
        {
            return string.Format("{0},{1}", FirstName, LastName);
        }
    }
}