Love array, understand Array

Source: Internet
Author: User

What is array?

Note:
Structs can be a collection of different types of data, and arrays can only be of the same type, so array is faster, structs can use complex data.
Arrays is a continuous set of data. Therefore, it can be accessed using consecutive integers or randomly. It is fast because it is stored in the continuous memory space.

Arrayrank:

Rank: If it is expressed in space, it can be said that it is a multi-dimensional trend, marking data like the coordinates of a map.
The most common int [] matrix = new int [5]; can be called a 1D array,
Int [,] matrix = new int [5, 5]; can be a two-dimensional array.
As the number of "," in [] increases, the number of rank increases at will.
Want to access data in array? Have you used a map?

Is it as simple or easier as a map.
Note: In general, the element label of the array starts from 0. This is true for single-dimension or multi-dimensional arrays. In special cases, you can define and use an array. Its element starts to indicate 1.

Array Bounds: Use array. Be sure to confirm bounds.

The getlength () method can be used to determine the length of a specific rank. If length is used, the length of all arrays is obtained, that is, length = rank1 * rank1leng + rank2 + rank2length ....

Comparing array and collections: this is what many people think of confuse.

When an array is created, its size and type are fixed and cannot be changed. For example, create int [] matrix = new int [5];
The length of matrix is five, one-dimensional, and the data type that can be stored can only be Int.
Collections can store complex data types and add or delete stored data at will.
An array instance cannot be const,
Const int [] matrix = {1, 2, 3}; // No,
However
Readonly int [] matrix = {1, 2, 3}; // yes, but the elements of matrix are read-only,
Think of Matrix [0] ++; // Error
When readonly is used to define an array, readonly will affect array variable itself (matrix), rather than array instance's element (Matrix [0], matrix [1]). that is, it cannot be directed to another array instance,
Realonly int [] matrix = {4, 2 };
Matrix = new int [2] {4, 2}; // No
Of course, you can also create a read-only arraylist.
Arraylist Al = new arraylist (9 );
Arraylist alreadonly = arraylist. readonly (Al );
Alreadonly [3] = 22; // No

Create an array instance
Because array is a reference type, you must use new when declaring an instance. At the same time, you must declare all rank values. After the array instance is declared, the compiler will assign values to each element of array instance based on the element type. For example, if int is 0, bool is false.

For example:
Long [] matrix = new long [4];
Execution is equivalent
Long [] matrix = new long [4];
Long [0] = 0l;
Long [1] = 0l;
Long [2] = 0l;
Long [3] = 0l;
At the same time, the compiler will put the array into the continuous memory, regardless of the type or length of the array, if a new int [2, 3, 4] is created, this array has 2*3*4 elements, all of which are in the same continuous memory.

Initialize the value of element in array


You can use "{", "}", "," to initialize the array element. The execution sequence is left to right.
For multidimen1_array, each element must be assigned a value.

Create computed size Array

When creating a multidimention array, you can use run-time expressions to define the length of the each dimention, as shown in figure

System. Console. writeline ( " Enter number of rows: " );
String S1 = System. Console. Readline ();
Int Rows = Int . Parse (S1 );
System. Console. writeline ( " Enter number of columns: " );
String S2 = System. Console. Readline ();
Int Cols = Int . Parse (S2 );

Int [,] Matrix = New Int [Rows, cols];

Similarly, you can use compile-time constants and run-time expression to define an array
For example:

System. Console. writeline ( " Enter number of rows: " );
String S1 = System. Console. Readline ();
Int Rows = Int . Parse (S1 );

Int [,] Matrix = New Int [Rows, 4 ];

However, there are limits, that is, it is impossible to use the run-time expression to define the array and initialize the array element, as shown in

String S = System. Console. Readline ();
Int Size = Int . Parse (s );
Int [] Data = New Int [Size] {0,1,2,3} ; // Not Allowed

Copy array variables:


When copying array, copy only variable, not instance, that is, both array variable point to the same memory,

For

Long [] Row = New Long [ 4 ];
Long [] Copy = Row;

Only two arrays variable point to the same memory area,
For

Row [ 0 ] ++ ; // Array instance's first element ++
Int Value = Copy [ 0 ]; // Assign the first element of arrayinstance to value
Console. writeline (value ); // Output variable

So value = 1.

Array property:

Rand property is a read-only integer value used to describe the dimension of array,

Int [] One = New Int [ 1 ];
Int [,] Two = New Int [ 1 , 2 ];
Int [,] Three = New Int [ 1 , 2 , 3 ];
// The resulting rank values are as follows:
One. Rank = 1
Two. Rank = 2
Three. Rank = 3

Length is also a read-only integer value, used to describe the total length of array,

One. Length = 1
Two. Length = 1 * 2
Three. Length = 1 * 2 * 3

Array methods:


Sort method:
Sort () is used for structures and classes that support icomparable.

Int [] Data = {4,6,3,8,9,3} ; // Unsorted
System. array. Sort (data ); // Now sorted

Clear method:
Clear (), you can set the element of a certain arrange of array to 0 (Value Type) or null (refrence type );

Int [] Data = {4,6,3,8,9,3} ;
System. array. Clear (data, 0 , Data. Length );

Clone method:
Clone () creates a new array instance based on each element of the source array instance. Modifying the array after clone () does not affect the source array.
For the clone () of the above blue font, it exists in the array instance method, not the static method.

Getlength method:
Getlength () can be used to obtain the length of a specific dimension.

Int [,] Data = { {0,1,2,3} , {4,5,6,7} } ;
Int Dim0 = Data. getlength ( 0 ); // = 2
Int Dim1 = Data. getlength ( 1 ); // = 4

Indexof method:
Indexof () can only be used in one dimension array to return the location of the first qualified element.

Int [] Data = {4,6,3,8,9,3} ;
Int Where = System. array. indexof (data, 9 ); // = 4

Note: For a specific array type, you may need to rewrite equal () to use the indexof method. For example, if you create a complex stucture array, You need to override equal () tell the compiler how to compare.

In addition, there are also useful methods such as reverse () -- reverse, lastindexof () -- The last configured index.

The original works are better written. I just made an excerpt. If you are interested, you must check the original works.
Refer:
Microsoft msdn Training 2001
Introduction to C # Programming
For the Microsoft. NET platform
(Prerelease)
Work book
Course number: 2124a

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.