数组Arrays

数组是一种数据结构,其中包含许多通过计算索引访问的变量。An array is a data structure that contains a number of variables that are accessed through computed indices. 数组中的变量(亦称为数组的元素)均为同一种类型,我们将这种类型称为数组的元素类型The variables contained in an array, also called the elements of the array, are all of the same type, and this type is called the element type of the array.

数组类型是引用类型,声明数组变量只是为引用数组实例预留空间。Array types are reference types, and the declaration of an array variable simply sets aside space for a reference to an array instance. 实际的数组实例是在运行时使用 new 运算符动态创建而成。Actual array instances are created dynamically at runtime using the new operator. new 运算指定了新数组实例的长度,然后在此实例的生存期内固定使用这个长度。The new operation specifies the length of the new array instance, which is then fixed for the lifetime of the instance. 数组元素的索引介于 0Length - 1 之间。The indices of the elements of an array range from 0 to Length - 1. new 运算符自动将数组元素初始化为其默认值(例如,所有数值类型的默认值为 0,所有引用类型的默认值为 null)。The new operator automatically initializes the elements of an array to their default value, which, for example, is zero for all numeric types and null for all reference types.

以下示例创建 int 元素数组,初始化此数组,然后打印输出此数组的内容。The following example creates an array of int elements, initializes the array, and prints out the contents of the array.

using System;
class ArrayExample
{
    static void Main() 
    {
        int[] a = new int[10];
        for (int i = 0; i < a.Length; i++) 
        {
            a[i] = i * i;
        }
        for (int i = 0; i < a.Length; i++) 
        {
            Console.WriteLine($"a[{i}] = {a[i]}");
        }
    }
}

上面的示例创建一维数组,并对其执行运算。This example creates and operates on a single-dimensional array. C# 还支持多维数组C# also supports multi-dimensional arrays. 数组类型的维数(亦称为数组类型的)是 1 与数组类型方括号内的逗号数量相加的结果。The number of dimensions of an array type, also known as the rank of the array type, is one plus the number of commas written between the square brackets of the array type. 以下示例分别分配一维、二维、三维数组。The following example allocates a single-dimensional, a two-dimensional, and a three-dimensional array, respectively.

int[] a1 = new int[10];
int[,] a2 = new int[10, 5];
int[,,] a3 = new int[10, 5, 2];

a1 数组包含 10 个元素,a2 数组包含 50 个元素 (10 × 5),a3 数组包含 100 个元素 (10 × 5 × 2)。The a1 array contains 10 elements, the a2 array contains 50 (10 × 5) elements, and the a3 array contains 100 (10 × 5 × 2) elements. 数组的元素类型可以是任意类型(包括数组类型)。The element type of an array can be any type, including an array type. 包含数组类型元素的数组有时称为交错数组,因为元素数组的长度不必全都一样。An array with elements of an array type is sometimes called a jagged array because the lengths of the element arrays do not all have to be the same. 以下示例分配由 int 数组构成的数组:The following example allocates an array of arrays of int:

int[][] a = new int[3][];
a[0] = new int[10];
a[1] = new int[5];
a[2] = new int[20];

第一行创建包含三个元素的数组,每个元素都是 int[] 类型,并且初始值均为 nullThe first line creates an array with three elements, each of type int[] and each with an initial value of null. 后面的代码行将这三个元素初始化为引用长度不同的各个数组实例。The subsequent lines then initialize the three elements with references to individual array instances of varying lengths.

通过 new 运算符,可以使用数组初始值设定项(在分隔符 {} 内编写的表达式列表)指定数组元素的初始值。The new operator permits the initial values of the array elements to be specified using an array initializer, which is a list of expressions written between the delimiters { and }. 以下示例分配 int[],并将其初始化为包含三个元素。The following example allocates and initializes an int[] with three elements.

int[] a = new int[] {1, 2, 3};

请注意,可从 { 和 } 内的表达式数量推断出数组的长度。Note that the length of the array is inferred from the number of expressions between { and }. 局部变量和字段声明可以进一步缩短,这样就不用重新声明数组类型了。Local variable and field declarations can be shortened further such that the array type does not have to be restated.

int[] a = {1, 2, 3};

以上两个示例等同于以下示例:Both of the previous examples are equivalent to the following:

int[] t = new int[3];
t[0] = 1;
t[1] = 2;
t[2] = 3;
int[] a = t;