At last it was another day. I got up, stretched out, and sat in front of my computer. today, I want to talk about the Array (Arrays) of C ). arrays in C # are counted from 0, just like some other excellent languages. We can see from the previous examples that, the first element of an array is a [0], rather than a (1) Like VB ). although this is the case, you still need to pay attention to some differences.
When declaring an array, square brackets must follow the type instead of the variable name, for example:
Int [] table; // cannot be written as int table []
This is obviously different from JAVA. in JAVA, this is acceptable.
You can also choose not to specify the size of the array in C #, which is different from the C language. This allows you to specify an array of any length, as shown below:
Int [] numbers; // Its length is arbitrary
Of course, you can also specify its size:
Int [10] numbers; // specifies an array with a length of 10.
In C #, the supported arrays include single-dimension arrays, multi-dimensional arrays, and multiple arrays. Their declaration methods are as follows:
Single-Dimension Array:
Int [] numbers;
Multi-dimensional array:
String [,] names;
Multiple Arrays:
Byte [] [] scores;
Declaring an array does not mean that it has been created. in C #, all array elements are objects (inverted! So before creating a java sdk, instantiate it as follows:
Single-Dimension Array:
Int [] numbers = new int [5];
Multi-dimensional array:
String [,] names = new string [5, 4];
Multiple Arrays:
Byte [] [] scores = new byte [5] [];
For (int x = 0; x <scores. Length; x ++)
{
Scores [x] = new byte [4];
}
Well, it's a bit strange. You don't need to worry about it first.
We can also create a larger array, such as a three-dimensional array:
Int [,] buttons = new int [4, 5, 3];
We can even mix multi-dimensional arrays and multi-dimensional arrays. The following example illustrates these:
Int [] [,] [,] numbers;
The following example shows how to build an array:
000: // Arraysarrays. cs
001: using System;
002: class DeclareArraysSample
003 :{
004: public static void Main ()
005 :{
006: // Single-dimen1_array
007: int [] numbers = new int [5];
008:
009: // multidimen1_array
010: string [,] names = new string [5, 4];
011:
012: // Array-of-arrays (jarged array)
013: byte [] [] scores = new byte [5] [];
014:
015: // Create the jarged array
016: for (int I = 0; I <scores. Length; I ++)
017 :{
018: scores [I] = new byte [I + 3];
019 :}
020:
021: // Print length of each row
022: for (int I = 0; I <scores. Length; I ++)
023 :{
024: Console. WriteLine ("Length of row {0} is {1}", I, scores [I]. Length );
025 :}
026 :}
027 :}
Its output is:
Length of row 0 is 3
Length of row 1 is 4
Length of row 2 is 5
Length of row 3 is 6
Length of row 4 is 7
The array initialization in C # can be initiated immediately after it is created. Like JAVA and C, the array Initialization is {}. of course, obviously, your initialization value must be the same as the array type you declare. For example, if you define an int type, you cannot give it a String. Alas, JAVA looks much more. in C #, the String should be written as a string. Otherwise, an error occurs again. SUNWEN may make such an error in subsequent courses. I hope you can correct it. haha!
The following example illustrates array initialization:
Int [] numbers = new int [5] {1, 2, 3, 4, 5 };
String [] names = new string [3] {"Matt", "Joanne", "Robert "};
You can also omit the array size, such:
Int [] numbers = new int [] {1, 2, 3, 4, 5 };
String [] names = new string [] {"Matt", "Joanne", "Robert "};
You can even omit the new language name. If you give the value:
Int [] numbers = {1, 2, 3, 4, 5 };
String [] names = {"Matt", "Joanne", "Robert "};
In C #, the access to arrays is the same as that to C/C ++/JAVA. The following statement creates an array and assigns its fifth element to 5:
Int [] numbers = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
Numbers [4] = 5;
If you do not have C/JAVA/C ++ programming experience, SUNWEN reminds me that numbers [4] indicates the fifth element of this array, as I have already said before, the array is counted from 0, so 0, 1, 2, 3, 4 is the fifth, so .... (stage: Stupid, you think we don't know. Come on !)
The following example is about multi-dimensional arrays:
Int [,] numbers = {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10 }};
Numbers [1, 1] = 5;
Again, note that all arrays in C # Are objects (faint, version d). Therefore, you can use the method of accessing objects to access arrays. and System. array is the abstraction of arrays. you can refer to the document to see the methods supported by the Array class. for example, you can use the length attribute to access the length of an array. for example:
Int [] numbers = {1, 2, 3, 4, 5 };
Int LengthOfNumbers = numbers. Length;
Oh, well, I have finished another lesson. It's 09:16 A.M. Beijing time now. I have to take a break! Haha! Coming soon!