数组问题

数组:一组包含相同类型的变量值。(这里以int类型为例)

源文件http://pan.baidu.com/share/link?shareid=426222&uk=3912660076

一维数组:

  声明,创建, 初始化和输出。

            //一维数组
            //1.声明一个
            int[] array1 = new int[8];
            foreach (int item in array1)
            {
                Console.Write(" " + item);
            }
            Console.WriteLine();

            //2.
            int[] array2;
            array2 = new int[8];
            foreach (int item in array2)
            {
                Console.Write(" " + item);
            }
            Console.WriteLine();

            //3.声明多个
            int[] array3 = new int[8], array4 = new int[8];
            foreach (int item in array4)
            {
                Console.Write(" " + item);
            }
            Console.WriteLine();

            //4.初始化
            int[] array5 = { 1, 2, 3, 4, 5, 6, 7, 8 };
            foreach (int item in array5)
            {
                Console.Write(" " + item);
            }
            Console.WriteLine();

            //5.
            for (int i = 0; i < array3.Length; i++)
            {
                array3[i] = i;
                Console.Write(" " + array3[i]);
            }
            Console.WriteLine();

            //6.
            int j = 0;
            foreach (int item in array1)
            {
                array1[item] = ++j;
                Console.Write(" " + array1[item]);
            }
            Console.WriteLine();

            //7.隐式类型初始化数组
            var array6 = new[] { 1, 2, 3, 4, 5, 6, 7, 8 };
            foreach (int item in array6)
            {
                Console.Write(" " + item);
            }
            Console.WriteLine();
            //8.
            int[] array7;
            array7 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
            foreach (int item in array7)
            {
                Console.Write(" " + item);
            }
            Console.WriteLine();

二(多)维数组:

    声明,创建, 初始化和输出。

  矩形数组

    注意:矩形数组的每一行要长度相同,否则会出现错误

            //矩形数组
            //1.
            int[,] array8 = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 } };//两行四列
            //2.
            int[,] array9 = new int[2, 3];
            for (int i = 0; i < array9.GetLength(0); i++)
            {
                for (int j = 0; j < array9.GetLength(1); j++)
                {
                    //array9[i][j] = j;//错误!!!
                    array9[i, j] = j;
                    Console.Write(" " + array9[i, j]);
                }
                Console.WriteLine();
            }
            Console.WriteLine();
            //3.
            int[,] array10;
            array10 = new int[3, 4];
            //4.
            int[,] array11 = new int[,] { { 1, 2, 3, 4}, { 5, 6, 7, 8 } };
            //错误!!!矩形数组不能用foreach.
            //foreach (int row in array11)
            //{
            //    foreach (int col in row)
            //    {
            //        Console.Write(" " + col);
            //    }
            //}
            foreach (int i in array11)
            {
                for (int j = 0; j < array11.GetLength(1); j++)
                {
                    //array11[i, j] = j;
                    Console.Write(" " + i);
                }
                Console.WriteLine();
            }
            Console.WriteLine();
            //5.
            int[,] array12 = new[,] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 } };
            for (int i = 0; i < array12.GetLength(0); i++)
            {
                foreach (int j in array12)
                {
                    //array11[i, j] = j;
                    Console.Write(" " + j);
                }
                Console.WriteLine();
            }
            Console.WriteLine();

  齿状数组

            //齿状数组
            //1.
            //int[][] array13=new int[2][3];//错误!!!
            //2.
            int[][] array14 ={new int[] {1},
                              new int[] {1,2},
                              new int[] {1,2,3}};//
            foreach (int[] row in array14)
            {
                foreach (int col in row)
                {
                    Console.Write(" " + col);
                }
                Console.WriteLine();
            }
            //3.
            int[][] array15;
            array15 = new int[3][];//创建3行
            array15[0] = new int[1];
            array15[1] = new int[2];
            array15[2] = new int[3];
            foreach (var row in array14)
            {
                foreach (int col in row)
                {
                    row[col - 1] = col;
                    Console.Write(" " + row[col - 1]);
                }
                Console.WriteLine();
            }
            //4.
            int[][] array16;
            array16 = new int[3][];
            array16[0] = new int[] { 1 };
            array16[1] = new int[] { 1, 2 };
            array16[2] = new int[] { 1, 2, 3 };
            for (int i = 0; i < array16.Length; i++)
            {
                for (int j = 0; j < array16[i].Length; j++)
                {
                    // array16[i, j] = j;//错误!!!
                    Console.Write(" " + array16[i][j]);
                }
                Console.WriteLine();
            }
            for (int i = 0; i < array16.Length; i++)
            {
                foreach (var item in array16[i])
                {
                    Console.Write(" " + item);
                }
                Console.WriteLine();
            }
            foreach (int[]/*var*/ item in array16)
            {
                for (int i = 0; i <item.Length; i++)
                {
                    Console.Write(" " + item[i]);
                }
                Console.WriteLine();
            }
            Console.ReadKey();

原文地址:https://www.cnblogs.com/wjshan0808/p/3040020.html