C # arrays study notes (beginner)

An array is a collection of one kind composed by several variables, array variables contained in the array elements referred to, they have the same type. Array elements can be of any type, but without a name, only through the index (also known as a subscript indicates the position number) to access. The number of elements is called a one-dimensional array of one-dimensional array length.

1. one-dimensional array

And creating a statement of a general form such as dimensional array:
Array Type [] = new Array array type name [array length]
initialized in three ways:

  1. When you create an array, initialization.
    ◼ array type [] = new Array array type name [array length] The initial list of values {}
    Eg of:
    int [] A = new int [] {. 3,. 4,. 5}; // recommended that a more code readable better
    or int [] a = {3, 4, 5};

  2. First statement after initialization.
    C # allows to declare a one-dimensional array, and then initialize each array element. Its general form:
    ◼ array type [] array name;
    ◼ array name = new array type [array length] {initialization list};
    Eg of:
    int [] A;
    A = new int [. 3] {. 3,. 4,. 5} ;

  3. Create, after initialization.
    C # allows to declare and create a one-dimensional array, and then initialize the array elements one by one. Its general form:
    ◼ array type [] array name = new array type [array length];
    ◼ array element = value;
    Eg of:
    int [] A = new int [. 3];
    A [0] =. 3;
    A [. 1] . 4 =;
    A [2] =. 5;

    Arrays are a number of array elements. Each array element is equivalent to a normal variable, you can change its value, it can also refer to its value. The general form of the use of array elements: array name [index]

    Operation of the one-dimensional array
    ◆ C # array type from the abstract base type System.Array derived.
    ◆ Array Length Attribute class returns an array of length.
    ◆ Array class member methods: Clear, Copy, Sort, Reverse , IndexOf, LastIndexOf, Resize the like, are used for replication of the array to clear the array elements, to sort the array, the array elements reverse the order, from left to right to find array elements, from right to left to find the array element, the array length changes.
    ◆ Sort, Reverse, IndexOf, LastIndexOf , Resize can only operate for a one-dimensional array.

2. multidimensional array
declaration to create multidimensional arrays and the general form:
◆ array type [comma list] array = new Array type name [Dimensions Length list]
number comma delimited list of plus 1 is the number of dimensions
◆ that is, if the list is a comma comma, is called two-dimensional array;
◆ If two commas, then called three-dimensional array, and so on.
Dimension defined between each number ◆ length dimension in the length of the list, for the interval number comma.

Similar to the one-dimensional array:

  1. When you create an array, initialization.
    int [,] a = new int [2, 3] {{1, 2, 3}, {3, 2, 1}};
    or int [,] a = {{ 1, 2, 3}, {3, 2, 1}}; // this kind in addition, have the rest are new

  2. First statement after initialization.
    int [,] A;
    A = new new int [,] {{. 1, 2,. 3}, {. 3, 2,. 1}};

  3. Create, after initialization
    int [,] = A new new int [2,. 3];
    A [0, 0] =. 1;
    A [0,. 1] = 2; // ...

3. Array array
array type is an array composed of array of a plurality of arrays.
Declare an array array format is as follows:

 ◆  数组类型[维度][子数组维度] 数组名 =new 数组类型[维度长度][子数组维度] 	
 
 省略维度为一维数组,省略子数组的维度表示 子数组为一维数组。

Array array initialization also has a variety of ways, including the creation of initialization, the first statement after initialization. Wherein, when creating a length dimension initialization may be omitted.
For an array is an array type, according to the following format for each reference subarray element:

                  数组名[索引列表][索引列表]

int [] [,] a = new int [2] [,]; // Create a 2-dimensional array of a two-dimensional sub-array consisting of

When the array type array declaration, can not specify the length of the subarray.
EG:
int [] [] = A new new int [2] [. 3]; / * is wrong, because the data in the sub-array is not required uniform * /

Initialization :
Eg of:
int [] [] = A new new int [] [] {new new int [] {l, 2,3}, new new int [] {. 4,. 5,. 6}};

=
Int [] [] A = new new int [2] [];
A [0] = int [. 3] {. 1, 2,. 3} new new;
A [. 1] = new new int [. 3] {. 4,. 5,. 6 };
also allows the above a [1] = new int [ 4] = {4, 5, 6, 7};

Published an original article · won praise 0 · Views 17

Guess you like

Origin blog.csdn.net/qq_41941392/article/details/104636237
Recommended