The array initialization for C # is to place the initial value within the curly braces ({}) when declaring the array. If no initial value is specified, the array member is automatically initialized to the default initial value of the array type. Please read the properties of the array. If an array is declared as a field of a type, it is set to the default value NULL when the type is instantiated.
Initialization of one or one-D arrays
1. Initialize the array when it is declared. Such as:
int[] array = new int[6]{1,2,3,4,5,6};
string[] Stringarray = new string[3]{"string1", "string2", "String3"};2. You can omit the size of the array. Such as:
int[] array = new int[]{1,2,3,4,5,6};
string[] Stringarray = new string[]{"string1", "string2", "String3"};3. If you provide an initializer, you can also omit the new operator. Such as:
Int[] array = {1,2,3,4,5,6};
String[] Stringarray = {"String1", "string2", "String3"};4. Declaring an array variable without initializing it, you must use the new operator when assigning the array to this variable. Such as:
Int[] Array;
Array = new int[]{1,2,3,4,5};
String[] Stringarray;
Stringarray = new string[]{"string1", "string2", "String3"}, and the initialization of multidimensional arrays
1. Initialize an array when it is declared. Such as:
int[,] array2d = new int[,]{{1,2},{3,4},{5,6}}; Two-dimensional arrays
Int[,,] Array3D = new int[,,]{{{1,2,3}},{{4,5,6}}; Three-dimensional array for a two-dimensional array array2d, this is a two-dimensional array of 3 rows and 2 columns. The 3 rows are three lines, {3,4} and {5,6}, each of which is enclosed in curly braces {}, the 1th row {0}, the 2nd line {3,4} is 1, and the 3rd line {5,6} is denoted by 2.
Each line in curly braces represents a column, and for Array2D, each row is 2 columns, denoted by 0, 1. Then this array is 3 rows and 2 columns.
So the element of Array2D's 3rd row 2 column is array2d[2][1]=6.
A two-dimensional array is a two-dimensional matrix, with only X, y two dimensions, like a plane, with strarr[x,y] to locate any element in the matrix. Traversing a two-dimensional matrix typically requires a two-layer loop.
Here are some examples of three-dimensional arrays:
Int[,,] Array3D = new int[,,]{
{{1,2,3,4},{5,6,7,8},{9,10,11,12}},
{{13,14,15,16},{17,18,19,20},{21,22,23,24}}
};
Equivalent to int[2,3,4].
? The first dimension is 4: refers to 4 elements within the innermost brace ({}). 1,2,3,4 or 5,,6,7,8, respectively.
? The second dimension is 3: Refers to the three curly braces in the second layer. The {1,2,3,4}, {5,6,7,8}, and {9,10,11,12} sections are respectively.
? The third dimension is 2: Refers to the two curly braces in the first layer. {{1,2,3,4},{5,6,7,8},{9,10,11,12}} and {{13,14,15,16},{17,18,19,20},{21,22,23,24}} respectively.
int[,] Array3D = new int[,,]{{{1,2,3}},{{4,5,6}},{{7,8,9}}}; equivalent to int[3,1,3].
int[,] Array3D = new Int[,,]{{{1},{2},{3}},{{4},{5},{6}},{{7},{8},{9}}}; equivalent to int[3,3,1].
int[,] Array3D = new int[,,]{{{1,2,3},{4,5,6},{7,8,9}}}; equivalent to int[1,3,3].
A three-dimensional array has three dimensions, like a cube, that needs to be strarr[x,y,z] to navigate to elements in the matrix, so if you need to traverse, you must use three nested loops.
An example of a simple overlay three-dimensional matrix is:
for (int i=0; i<x; i++)
{
for (int j=0; j<y; j + +)
{
for (int k=0; k<z; k++)
{
Console.WriteLine (Strarr[i, J, K]);
}
}
}
2. You do not need to specify a level when initializing an array. Such as:
int[,] Array2D = {{1,2},{3,4},{5,6}};3. Declaring an array variable without initializing it, you must use the new operator when assigning the array to this variable. Such as:
int[,] array2d;
Array2D = new int[,]{{1,2},{3,4},{5,6}};4. Initializes the array variable to the default value (except for jagged arrays). Such as:
int[,] array = new int[2,3];5. You can assign values to the elements of an array. Such as:
array[3,4] = 10;
Three, the initialization of the staggered array
1. Declare a one-dimensional array, and the 5 elements in the array are all one-dimensional arrays of integers:
int[][] Jiaoarray = new int[5][]; Initializes the elements of the Jiaoarray array:
Jiaoarray[0] = new INT[10];
JIAOARRAY[1] = new INT[9];
JIAOARRAY[2] = new INT[8];
JIAOARRAY[3] = new INT[7];
JIAOARRAY[4] = new INT[6]; The element is initialized and can be used.
2. Use an initializer to populate the array elements with values, and you do not need to set the array size. Such as:
Jiaoarray[0] = new int[]{1,2,3,4,5}; Initializes the first element of the array: an array of 5 integers
JIAOARRAY[1] = new int[]{2,4,6,8}; Initializes the second element of the array: an array of 4 integers
JIAOARRAY[2] = new int[]{1,3,5}; Initializes the third element of the array: an array of 3 integers
3. Initialize when an array is declared. Such as:
int[][] Jiaoarray = new int[][]
{
New int[]{1,2,3,4,5},
New int[]{2,4,6,8},
New int[]{1,3,5}
};
4. You can use the following format:
int[][] Jiaoarray =
{
New int[]{1,2,3,4,5},
New int[]{2,4,6,8},
New int[]{1,3,5}
};
5. The elements of a jagged array are reference types and are initialized to null. We can access individual array elements:
JIAOARRAY[0][1] = 5; Assigns a 5 to the 2nd element of the 1th array [0] [1]
JIAOARRAY[3][2] = 7; Assigns a 7 to the 3rd element of the 4th array [3] [2]
6. You can mix jagged and multidimensional arrays. The following declares and initializes a one-dimensional jagged array that contains two-dimensional array elements of different sizes:
int[][,] Jiaoarray = new int[3][,]
{
New int[,]{{1,2},{3,4}},
New Int[,]{{11,22},{33,44},{55,66}},
New int[,]{{111,222},{333,444},{555,666}}
};
We can access individual elements, the following example shows the value of the first array element [1,0] 3:
Console.WriteLine ("{0}", jiaoarray[0][1,0]); The length method returns the number of arrays contained in a jagged array.
Console.WriteLine (Jiaoarray.length); return value 3
Iv. examples
Example One
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace Test
{
Class Program
{
static void Main (string[] args)
{
Initialization of C # jagged arrays-www.baike369.com
Jagged array: Declares a one-dimensional array of 2 elements, all of which are arrays of one-dimensional integers
int[][] array = new int[2][];
Array[0] = new Int[4] {1, 2, 3, 4}; Initializing elements of an array
ARRAY[1] = new Int[5] {0, 2, 4, 6, 8}; Initializing elements of an array
for (int i = 0; i < array. Length; i++)
{
Console.Write ("element ({0}):", I);//Displays the elements of a jagged array
Displays the value of each element in a jagged array, which is a one-dimensional array of integers
for (int j = 0; J < Array[i]. Length; J + +)
{
Console.Write ("{0}{1}", Array[i][j],
j== (Array[i]. Length-1)? "": "");
}
Console.WriteLine (""); Wraps the elements of each jagged array
}
Console.ReadLine ();
}
}
}
Operation Result:
Element (0): 1 2 3 4
Element (1): 0 2 4 6 8 of which
j== (Array[i]. Length-1)? "": "" adds a space between the values of the elements in a jagged array, that is, the elements of a one-dimensional array of integers. Please read the C # conditional operator (?:).
Example Two
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace Test
{
Class Program
{
static void Main (string[] args)
{
Two-dimensional arrays
int[,] array = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
int array1 = array[3, 1];
Three-dimensional arrays
Int[,,] Array3D = new int[,,]{
{{1,2,3,4},{5,6,7,8},{9,10,11,12}},
{{13,14,15,16},{17,18,19,20},{21,22,23,24}}
};
int array2 = array3d[1, 2, 3];
Console.WriteLine ("array[3,1" (two-dimensional array): {0} ", array1);
Console.WriteLine ("array3d[1,2,3" (three-dimensional array): {0} ", array2);
Console.ReadLine ();
}
}
}
Operation Result:
array[3,1] (two-dimensional array): 8
array3d[1,2,3] (three-dimensional array): 24
C # Array Initialization