zoukankan      html  css  js  c++  java
  • c#数组类型

    数组类型

    在 C# 中,数组实际上是对象,数组是一种数据结构,它包含若干相同类型的变量。

    数组概述

    数组具有以下属性:

    • 数组可以是 一维多维交错的。
    • 数值数组元素的默认值设置为零,而引用元素的默认值设置为 null。
    • 交错数组是数组的数组,因此其元素是引用类型并初始化为 null。
    • 数组的索引从零开始:具有 n 个元素的数组的索引是从 0 到 n-1。
    • 数组元素可以是任何类型,包括数组类型。
    • 数组类型是从抽象基类型 Array 派生的 引用类型。 由于此类型实现了 IEnumerableIEnumerable< T> ,因此可以对 C# 中的所有数组使用foreach 迭代。

    一维数组

    int[] array = new int[5];
    string[] stringArray = new string[6];
    

    此数组包含从 array[0] 到 array[4] 的元素。 new 运算符用于创建数组并将数组元素初始化为它们的默认值。 在此例中,所有数组元素都初始化为零。

    数组初始化

    int[] array1 = new int[] { 1, 3, 5, 7, 9 };
    string[] weekDays = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
    

    多维数组

    数组可以具有多个维度。 例如,下列声明创建一个四行两列的二维数组。

    int[,] array = new int[4, 2];
    

    声明创建一个三维(4、2 和 3)数组。

    int[, ,] array1 = new int[4, 2, 3];
    

    数组初始化

    // Two-dimensional array.
    int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
    // The same array with dimensions specified.
    int[,] array2Da = new int[4, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
    // A similar array with string elements.
    string[,] array2Db = new string[3, 2] { { "one", "two" }, { "three", "four" },
                                            { "five", "six" } };
    
    // Three-dimensional array.
    int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } }, 
                                     { { 7, 8, 9 }, { 10, 11, 12 } } };
    // The same array with dimensions specified.
    int[, ,] array3Da = new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } }, 
                                           { { 7, 8, 9 }, { 10, 11, 12 } } };
    
    // Accessing array elements.
    System.Console.WriteLine(array2D[0, 0]);
    System.Console.WriteLine(array2D[0, 1]);
    System.Console.WriteLine(array2D[1, 0]);
    System.Console.WriteLine(array2D[1, 1]);
    System.Console.WriteLine(array2D[3, 0]);
    System.Console.WriteLine(array2Db[1, 0]);
    System.Console.WriteLine(array3Da[1, 0, 1]);
    System.Console.WriteLine(array3D[1, 1, 2]);
    
    // Output:
    // 1
    // 2
    // 3
    // 4
    // 7
    // three
    // 8
    // 12
    

    也可以初始化数组但不指定级别。

    int[,] array4 = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
    

    如果选择声明一个数组变量但不将其初始化,必须使用 new 运算符将一个数组分配给此变量。 以下示例显示 new 的用法。

    int[,] array5;
    array5 = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };   // OK
    //array5 = {{1,2}, {3,4}, {5,6}, {7,8}};   // Error
    

    将值分配给特定的数组元素。

    array5[2, 1] = 25;
    

    将数组元素初始化为默认值(交错数组除外):

    int[,] array6 = new int[10, 10];
    

    交错数组

    交错数组是元素为数组的数组。 交错数组元素的维度和大小可以不同。 交错数组有时称为“数组的数组”。

    int[][] jaggedArray = new int[3][];
    

    必须初始化 jaggedArray 的元素后才可以使用它。

    jaggedArray[0] = new int[5];
    jaggedArray[1] = new int[4];
    jaggedArray[2] = new int[2];
    

    每个元素都是一个一维整数数组。 第一个元素是由 5 个整数组成的数组,第二个是由 4 个整数组成的数组,而第三个是由 2 个整数组成的数组。

    jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 };
    jaggedArray[1] = new int[] { 0, 2, 4, 6 };
    jaggedArray[2] = new int[] { 11, 22 };
    

    声明数组时将其初始化

    int[][] jaggedArray2 = new int[][] 
    {
        new int[] {1,3,5,7,9},
        new int[] {0,2,4,6},
        new int[] {11,22}
    };
    

    访问个别数组元素:

    // Assign 77 to the second element ([1]) of the first array ([0]):
    jaggedArray3[0][1] = 77;
    
    // Assign 88 to the second element ([1]) of the third array ([2]):
    jaggedArray3[2][1] = 88;
    

    数组使用 foreach

    创建一个名为 numbers 的数组,并用 foreach 语句循环访问该数组:

    int[] numbers = { 4, 5, 6, 1, 2, 3, -2, -1, 0 };
    foreach (int i in numbers)
    {
        System.Console.Write("{0} ", i);
    }
    // Output: 4 5 6 1 2 3 -2 -1 0
    

    多维数组,可以使用相同方法来循环访问元素

    int[,] numbers2D = new int[3, 2] { { 9, 99 }, { 3, 33 }, { 5, 55 } };
    // Or use the short form:
    // int[,] numbers2D = { { 9, 99 }, { 3, 33 }, { 5, 55 } };
    
    foreach (int i in numbers2D)
    {
        System.Console.Write("{0} ", i);
    }
    // Output: 9 99 3 33 5 55
    

    一维数组传递给方法

    int[] theArray = { 1, 3, 5, 7, 9 };
    PrintArray(theArray);
    

    print 方法的部分实现。

    void PrintArray(int[] arr)
    {
        // Method code.
    }
    

    初始化和传递新数组

    PrintArray(new int[] { 1, 3, 5, 7, 9 });
    

    官方示例

    class ArrayClass
    {
        static void PrintArray(string[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                System.Console.Write(arr[i] + "{0}", i < arr.Length - 1 ? " " : "");
            }
            System.Console.WriteLine();
        }
    
        static void ChangeArray(string[] arr)
        {
            // The following attempt to reverse the array does not persist when
            // the method returns, because arr is a value parameter.
            arr = (arr.Reverse()).ToArray();
            // The following statement displays Sat as the first element in the array.
            System.Console.WriteLine("arr[0] is {0} in ChangeArray.", arr[0]);
        }
    
        static void ChangeArrayElements(string[] arr)
        {
            // The following assignments change the value of individual array 
            // elements. 
            arr[0] = "Sat";
            arr[1] = "Fri";
            arr[2] = "Thu";
            // The following statement again displays Sat as the first element
            // in the array arr, inside the called method.
            System.Console.WriteLine("arr[0] is {0} in ChangeArrayElements.", arr[0]);
        }
    
        static void Main()
        {
            // Declare and initialize an array.
            string[] weekDays = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
    
            // Pass the array as an argument to PrintArray.
            PrintArray(weekDays);
    
            // ChangeArray tries to change the array by assigning something new
            // to the array in the method. 
            ChangeArray(weekDays);
    
            // Print the array again, to verify that it has not been changed.
            System.Console.WriteLine("Array weekDays after the call to ChangeArray:");
            PrintArray(weekDays);
            System.Console.WriteLine();
    
            // ChangeArrayElements assigns new values to individual array
            // elements.
            ChangeArrayElements(weekDays);
    
            // The changes to individual elements persist after the method returns.
            // Print the array, to verify that it has been changed.
            System.Console.WriteLine("Array weekDays after the call to ChangeArrayElements:");
            PrintArray(weekDays);
        }
    }
    // Output: 
    // Sun Mon Tue Wed Thu Fri Sat
    // arr[0] is Sat in ChangeArray.
    // Array weekDays after the call to ChangeArray:
    // Sun Mon Tue Wed Thu Fri Sat
    // 
    // arr[0] is Sat in ChangeArrayElements.
    // Array weekDays after the call to ChangeArrayElements:
    // Sat Fri Thu Wed Thu Fri Sat
    

    多维数组传递给方法

    int[,] theArray = { { 1, 2 }, { 2, 3 }, { 3, 4 } };
    Print2DArray(theArray);
    

    该方法接受一个二维数组作为其参数。

    void Print2DArray(int[,] arr)
    {
        // Method code.
    }
    

    初始化和传递新数组

    Print2DArray(new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } });
    

    官方示例

    class ArrayClass2D
    {
        static void Print2DArray(int[,] arr)
        {
            // Display the array elements.
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    System.Console.WriteLine("Element({0},{1})={2}", i, j, arr[i, j]);
                }
            }
        }
        static void Main()
        {
            // Pass the array as an argument.
            Print2DArray(new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } });
    
            // Keep the console window open in debug mode.
            System.Console.WriteLine("Press any key to exit.");
            System.Console.ReadKey();
        }
    }
        /* Output:
            Element(0,0)=1
            Element(0,1)=2
            Element(1,0)=3
            Element(1,1)=4
            Element(2,0)=5
            Element(2,1)=6
            Element(3,0)=7
            Element(3,1)=8
        */
    

    使用 ref 和 out 传递数组

    使用数组类型的 out 参数前必须先为其赋值,即必须由被调用方为其赋值。

    static void TestMethod1(out int[] arr)
    {
        arr = new int[10];   // definite assignment of arr
    }
    

    与所有的 ref 参数一样,数组类型的 ref 参数必须由调用方明确赋值。 因此不需要由接受方明确赋值。 可以将数组类型的 ref 参数更改为调用的结果。

    static void TestMethod2(ref int[] arr)
    {
        arr = new int[10];   // arr initialized to a different array
    }
    

    示例

    在调用方(Main 方法)中声明数组 theArray,并在 FillArray 方法中初始化此数组。 然后将数组元素返回调用方并显示。

    class TestOut
    {
        static void FillArray(out int[] arr)
        {
            // Initialize the array:
            arr = new int[5] { 1, 2, 3, 4, 5 };
        }
    
        static void Main()
        {
            int[] theArray; // Initialization is not required
    
            // Pass the array to the callee using out:
            FillArray(out theArray);
    
            // Display the array elements:
            System.Console.WriteLine("Array elements are:");
            for (int i = 0; i < theArray.Length; i++)
            {
                System.Console.Write(theArray[i] + " ");
            }
    
            // Keep the console window open in debug mode.
            System.Console.WriteLine("Press any key to exit.");
            System.Console.ReadKey();
        }
    }
        /* Output:
            Array elements are:
            1 2 3 4 5        
        */
    

    在调用方(Main 方法)中初始化数组 theArray,并通过使用 ref 参数将其传递给 FillArray 方法。 在 FillArray 方法中更新某些数组元素。 然后将数组元素返回调用方并显示

    class TestRef
    {
        static void FillArray(ref int[] arr)
        {
            // Create the array on demand:
            if (arr == null)
            {
                arr = new int[10];
            }
            // Fill the array:
            arr[0] = 1111;
            arr[4] = 5555;
        }
    
        static void Main()
        {
            // Initialize the array:
            int[] theArray = { 1, 2, 3, 4, 5 };
    
            // Pass the array using ref:
            FillArray(ref theArray);
    
            // Display the updated array:
            System.Console.WriteLine("Array elements are:");
            for (int i = 0; i < theArray.Length; i++)
            {
                System.Console.Write(theArray[i] + " ");
            }
    
            // Keep the console window open in debug mode.
            System.Console.WriteLine("Press any key to exit.");
            System.Console.ReadKey();
        }
    }
        /* Output:
            Array elements are:
            1111 2 3 4 5555
        */
    

    隐式类型的数组

    如何创建隐式类型的数组:

    class ImplicitlyTypedArraySample
    {
        static void Main()
        {
            var a = new[] { 1, 10, 100, 1000 }; // int[]
            var b = new[] { "hello", null, "world" }; // string[]
    
            // single-dimension jagged array
            var c = new[]   
    {  
        new[]{1,2,3,4},
        new[]{5,6,7,8}
    };
    
            // jagged array of strings
            var d = new[]   
    {
        new[]{"Luca", "Mads", "Luke", "Dinesh"},
        new[]{"Karen", "Suma", "Frances"}
    };
        }
    }
    

    创建包含数组的匿名类型时,必须在该类型的对象初始值设定项中对数组进行隐式类型化。 在下面的示例中,contacts 是一个隐式类型的匿名类型数组,其中每个匿名类型都包含一个名为 PhoneNumbers 的数组。 请注意,对象初始值设定项内部未使用 var 关键字。

    var contacts = new[] 
    {
        new {
                Name = " Eugene Zabokritski",
                PhoneNumbers = new[] { "206-555-0108", "425-555-0001" }
            },
        new {
                Name = " Hanying Feng",
                PhoneNumbers = new[] { "650-555-0199" }
            }
    };
    
  • 相关阅读:
    SpringMVC中静态获取request对象 Spring中获取 HttpServletRequest对象【转载】
    springcloud 的loadbalancer 轮询算法切换方法 2021.4.3
    springboot项目启动增加图标
    rabbitmq 端口作用以及修改方法
    centos8 安装rabbitmq
    springcloud config client Value获取不到信息的问题的处理方法
    springcloud config配置git作为数据源然后启动报错 If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    Sublime Text的列模式如何操作
    centos8 安装redis
    jQuery简单的Ajax调用
  • 原文地址:https://www.cnblogs.com/ouyangkai/p/12753831.html
Copyright © 2011-2022 走看看