Beginner with c # 5

zhaozj2021-02-08  278

1.5 Array Types

The array can be one-dimensional, but also multi-dimensional. The number of a few ancestors can be neat, or may be long (jagged).

One-dimensional array is the most common, the simplest. Here the value gives an example, there is not much explanation. * /

Using system;

Class test

{

Static void main () {

Int [] arr = new int [5];

For (int i = 0; i

Arr [i] = i * i;

For (int i = 0; i

Console.writeLine ("Arr [{0}] = {1}", I, Arr [i]);

}

}

/ * The result is as follows:

Arr [0] = 0

Arr [1] = 1

Arr [2] = 4

Arr [3] = 9

Arr [4] = 16

We can also look at the definitions and assignments of multi-dimensional, rules, and growing arrays: * /

Class test

{

Static void main () {

int [] a1 = new int = {1, 2, 3}; //

Int [,] a2 = new int [] {{1, 2, 3}, {4, 5, 6}}; // 2D

int [,,] a3 = new int in INT [10, 20, 30]; // 3D

int [] [] j2 = new int [3] []; // become long

J2 [0] = new int {1, 2, 3};

J2 [1] = new int od {1, 2, 3, 4, 5, 6};

J2 [2] = new int od [] {1, 2, 3, 4, 5, 6, 7, 8, 9};

}

}

/ *

The above example gives an array of various styles. Variables A1, A2 and A3 are regular arrays. J2 is a growing array.

The rule array is easy to calculate their length. For example, the length of A3 is: 10 * 20 * 30 = 6000. Conversely, become long

An array is a bit different, and each dimension must be individually defined. If the first dimension of J2 is 3, the second is 6, the first

Three are 9, so the total length is: 1 * 3 1 * 6 1 * 9 = 18.

The assignment of the array is a rigorous style. In some cases, we can simplify the writing method, but I always feel this simplification.

Application is limited, it is easy to go wrong. It is not introduced here. Here, give an example of explaining the parameters in the function.

Value * /

Class test

{

Static void f (long [] arr) {}

Static void main () {

F (new longt [] {1, 2, 3});

}

}

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

New Post(0)