Define arrays in C # -- string and Array Operations
I. One Dimension:
Int [] numbers = new int [] {1, 2, 3, 4, 5, 6}; // Variable Length
Int [] numbers = new int [3] {1, 2, 3}; // Fixed Length
Ii. Multidimensional
Int [,] numbers = new int [,] {1, 2, 3}, {1, 3}; // The length is not fixed.
Int [,] numbers = new int [2, 2] {1, 2}, {1, 2}; // Fixed Length
Iii. Example
A: int [] mf1 = new int [6];
// Pay attention to the range of the initialization array or specify the initial value. // a one-dimensional integer array containing six elements. The initial values are 1, 2, 3, 4, 5, and 6.
Int [] mf2 = new int [6] {1, 2, 3, 4, 5, 6 };
B: // a one-dimensional string array. If an initial value is provided, the new operator can be omitted.
String [] mf3 = {"C", "C ++", "C #"};
C: // One-dimensional object Array
Object [] mf4 = new object [5] {26, 27, 28, 29, 30 };
D: // two-dimensional integer array, initial value mf5 [] = 1, mf5 [] = 2, mf5 [] = 3, mf5 [] = 4
Int [,] mf5 = new int [,] {1, 2}, {3, 4 }};
E: // a two-dimensional integer array of 6*6
Int [,] mf6 = new MF [6, 6];
4. Obtain the number of array elements:
Int B;
B = sizeof (a)/sizeof (* );
C # string and Array Operations
String operation (take the current time)
String time = convert. tostring (datetime. Today). Split (New char [] {''}); textbox1.text = time [0]; use space as the demarcation point;
Array Overview
C # create an index for the array from scratch, that is, the array index starts from scratch. In C #, arrays work in a similar way as in most other popular languages. However, some differences should be noted.
When declaring an array, square brackets ([]) must follow the type, not the identifier. In C #, placing square brackets after identifiers is invalid syntax.
Int [] Table; // not int table [];
Another detail is that the size of an array is not a part of its type, but in C, it is a part of the array type. This allows you to declare an array and assign it any array of the int object regardless of the array length.
Int [] numbers; // declare numbers as an int array of any size
Numbers = new int [10]; // numbers is a 10-element array
Numbers = new int [20]; // now it's a 20-element array
Declare an array
C # supports one-dimensional arrays, multi-dimensional arrays (rectangular arrays), and arrays (staggered arrays ). The following example shows how to declare arrays of different types:
One-dimensional array: int [] numbers;
Multi-dimensional array: String [,] names;
Array array (staggered): byte [] [] scores;
Declaring arrays (as shown above) does not actually create them. In C #, arrays are objects (discussed later in this tutorial) and must be instantiated. The following example shows how to create an array:
One-dimensional array: int [] numbers = new int [5];
Multi-dimensional array: String [,] names = new string [5, 4];
Array array (staggered): byte [] [] scores = new byte [5] []; for (INT x = 0; x <scores. length; X ++) {scores [x] = new BYT [4];
}
You can also have a larger array. For example, you can have a three-dimensional array of rectangles: int [,] buttons = new int [, 3];
You can even mix a rectangle array with a staggered array. For example, the following code declares the one-dimensional array int [] [,] [,] numbers of a two-dimensional array of Type 'int;
Initialize an array
C # It provides a simple and straightforward method to initialize the array during declaration by including the initial value in braces. The following example shows how to initialize arrays of different types.
Note: If the array is not initialized at the time of declaration, the array members will be automatically initialized to the default initial value of the array type. In addition, if an array is declared as a field of a certain type, it is set to the default value null when it is instantiated.
One-dimensional array
Int [] numbers = new int [5] {1, 2, 3, 4, 5 };
String [] names = new string [3] {"Matt", "Joanne", "Robert "};
The array size can be omitted, as shown below:
Int [] numbers = new int [] {1, 2, 3, 4, 5 };
String [] names = new string [] {"Matt", "Joanne", "Robert "};
If you provide an initial value setting item, you can also omit the new operator, as shown below:
Int [] numbers = {1, 2, 3, 4, 5 };
String [] names = {"Matt", "Joanne", "Robert "};
Multi-dimensional array
Int [,] numbers = new int [3, 2] {1, 2}, {3, 4}, {5, 6 }};
String [,] siblings = new string [2, 2] {"Mike", "Amy" },{ "Mary", "Albert "}};
The array size can be omitted, as shown below:
Int [,] numbers = new int [,] {1, 2}, {3, 4}, {5, 6 }};
String [,] siblings = new string [,] {"Mike", "Amy" },{ "Mary", "Albert "}};
If you provide an initial value setting item, you can also omit the new operator, as shown below:
Int [,] numbers = {1, 2}, {3, 4}, {5, 6 }};
String [,] siblings = {"Mike", "Amy" },{ "Mary", "Albert "}};
Staggered array (array)
You can initialize the staggered array as shown in the following example:
Int [] [] numbers = new int [2] [] {New int [] {2, 3, 4}, new int [] {5, 6, 7, 8, 9 }};
The size of the first array can be omitted, as shown below:
Int [] [] numbers = new int [] [] {New int [] {2, 3, 4}, new int [] {5, 6, 7, 8, 9};-or-
Int [] [] numbers = {New int [] {2, 3, 4}, new int [] {5, 6, 7, 8, 9 }};
Note that there is no initialization syntax for elements in the staggered array.
Member Groups
The members of the member number can be directly performed, similar to those of the member number in C/C ++. For example, the following code creates an array named numbers and assigns 5 to the fifth element of the array:
Int [] numbers = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
Numbers [4] = 5;
The following Code declares a multi-dimensional array and assigns 5 to members located in [1, 1:
Int [,] numbers = {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10 }};
Numbers [1, 1] = 5;
The following declares a one-dimensional staggered array, which contains two elements. The first element is an array of two integers, and the second element is an array of three integers:
Int [] [] numbers = new int [] [] {New int [] {1, 2}, new int [] {3, 4, 5 }};
The following statement assigns 58 to the first element of the first array and 667 to the second element of the second array:
Numbers [0] [0] = 58;
Numbers [1] [1] = 667;
Array is an object
In C #, arrays are actually objects. System. array is the abstract base type of all array types. You can use the attributes of system. array and other class members. In this example, the Length attribute is used to obtain the length of the array. The following Code assigns the numbers array length (5) to a variable named lengthofnumbers:
Int [] numbers = {1, 2, 3, 4, 5 };
Int lengthofnumbers = numbers. length;
The system. array class provides many other useful methods/attributes, such as methods for sorting, searching, and copying arrays.
Use foreach for Arrays
C # also provides the foreach statement. This statement provides a simple and clear method to cyclically access the elements of the array. For example, the following code creates an array named numbers and cyclically accesses the Array Using the foreach statement:
Int [] numbers = {4, 5, 6, 1, 2, 3,-2,-1, 0 };
Foreach (int I in numbers) {system. Console. writeline (I );}
With multidimensional arrays, you can use the same method to cyclically access elements. For example:
Int [,] numbers = new int [3, 2] {9, 99}, {3, 33}, {5, 55 }};
Foreach (int I in numbers) {console. Write ("{0}", I );}
The output in this example is 9 99 3 33 5 55
However, with multidimensional arrays, nested for loops allow you to better control array elements.