C # advanced 3

zhaozj2021-02-17  52

Sunwen Tutorial ---- C # advanced

(three)

Mrfat@china.com

I finally got another day, get up, stretched a lazy waist, sitting in front of the computer. Today I want to tell you the array of C # Arrays. C #, the same, other excellent languages, also from 0 In the beginning, this can be seen from our previous example, that is, the first element of an array is A [0], not like VB's a (1). Although this, you still want Pay attention to some differences.

When declaring an array, square brackets must be followed behind, and cannot follow the variable name, such as: int [] table; // cannot be written into int Table [] This is clearly different from Java, in Java This is ok.

There is also the size of the array in C #, which is different from the C language. This allows you to specify array of arbitrary lengths, as follows: int [] NumBers; // It is any of course, You can also specify its size: int [10] Numbers; // Specify an array of length 10.

In C #, the supported arrays include: single-dimensional array, multi-dimensional array, multiple groups. Their declaration methods are as follows: single-dimensional array: int [] NumBers; multi-dimensional array: string [,] Names; multiple arrays: byte [] [ Score;

Declare an array does not mean it has been established. In C #, all array elements are objects (fell! How to speak with Java & *% $ # @), so before building it, first you want to instance Chemical: int [] NumBers = new int [5]; multi-dimensional array: string [,] names = new string [5,4]; multiple arrays:

BYTE [] [] score = new byte [5] [];

For (int x = 0; x

{

Scores [x] = new byte [4];

} Oh, this is a bit strange, I don't need to care, and I will say later.

We can also build a larger array, such as a three-dimensional array: int [,,] buttons = new int intellectual INT [4, 5, 3];

We can even mix multi-dimensional arrays and multiple groups, the following examples illustrate these: int [] [,,] [,] NumBers;

The following example shows all the above methods of constructing an array:

000: // arrays / arrays.cs

001: using system;

002: Class DeclaRRayssample

003: {

004: public static void main ()

005: {

006: // Single-Dimensional Array

007: int [] Numbers = new int [5];

008:

009: // Multidimensional Array

010: String [,] names = new string [5,4];

011:

012: // array-of-arrays (jagged array)

013: Byte [] [] score = new byte [5] [];

014:

015: // Create The Jagged Array

016: for (int i = 0; I

017: {

018: scorers [i] = new byte [i 3];

019:}

020:

021: // Print Length of Each ROW

022: for (int i = 0; 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

In initialization in the C # in the C #, it can be initialized, and Java and C are used, {}. Of course, it is obvious that your initialization value must be the same as the array of array you declared, such as you define an int type. You can't give it a string, hey, Java looks more, in C #, string should be written as string, otherwise, it is wrong. Sunwen may have such a mistake in the future course, but also hope everyone Finger. Hehe!

The following example illustrates the initialization of the array:

Int [] NumBers = new int [5] {1, 2, 3, 4, 5}; string [] names = new string [3] {"matt", "joanne", "robert"};

You can also omit the size of the array, such as:

Int [] NumBers = new int [] {1, 2, 3, 4, 5}; string [] names = new string [] {"matt", "joanne", "robert"}

You can even omit the new name, if you give a value:

int [] NumBers = {1, 2, 3, 4, 5};

String [] names = {"matt", "joanne", "robert"};

In C #, the number of access and C / C / Java are the same, the following statement establishes 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 don't have C / Java / C programming experience, then SUNWEN is reminded here that NumBers [4] indicates the fifth element of this array, because I have already said in front, the array starts from 0, So 0, 1, 2, 3, 4 is just the fifth, so .... (under the stage: stupid, you think we don't know, continue to say!)

The following example is about the multidimensional array:

INT [,] NumBers = {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}}; NumBers [1, 1] = 5;

Note that all arrands in C # are objects (Faint, D), so you can use access to objects to access arrays. And System.Array is an abstraction of arrays. You can see the documentation to see Array class support Method. For example, you can use the Length attribute to access the length of the array. Such examples:

int [] NumBers = {1, 2, 3, 4, 5};

INT lengthofnumbers = NumBers.Length; Hey, ok, finished, it is a lesson, now Beijing time at 9:16, I have to take a break! Haha! Wait!

Next page

转载请注明原文地址:https://www.9cbs.com/read-29751.html

New Post(0)