Array "Go" in C #

Source: Internet
Author: User

Directory

    • Declaration of an array
    • Initialization of the array
    • Access to arrays
    • Common properties and methods for arrays

For convenience, the following arrays are defined as integral types

   declaration of an array

C # When declaring an array, square brackets [] must follow the type, not after the identifier

    1. Declaring a one-dimensional array

The size of the C # array is not part of its type, but is part of the array type in the C language

int[4];    // Declare the array type and use the New keyword to allocate memory space for him/ * The above declaration can be decomposed into the following two steps *///int[// Use the New keyword to allocate memory space for him 

In addition, C # declares an array when the square brackets that support allocating memory are integer variables or variable expressions, as long as the value of the variable is determined beforehand (this is not allowed before the C C99 standard appears)

Int[n];    The value of the//n must be determined beforehand, even if the value of the array changing n is not changed after the declaration 

    2. Declaring a multidimensional array

int[4,// declaring an array of two-dimensional integers  

    3. Declaring a jagged array (an array of arrays)

    The size of a multidimensional array is rectangular, such as a 3x3 (or 4x3, just the same size as each row) element. And the size of the jagged array is more flexible, in the jagged array, each row can have a different size, such as the number of jagged

New int[3][];    // Note Unlike a two-dimensional array, there are two square brackets
  

Initialization of the array

  If you have initialization data, you can initialize it in the simplest form below.

Int[] Mysigarray = {1,2,3,4};//One-dimensional array initializationint[,] Mymularray = {{1,2,3},{4,5,6},{7,8,9}}; // two-dimensional array initialization int[][] Mysawarray = {new int[] { 1,2},new int[] {3,4,5,6,7,8},new int[] {9,10,11}}; Span style= "color: #008000;" >// jagged array initialization            

Initialize an array with the clear of array

byte[] tt = new byte[2147483591];
Array.clear (TT, 0, TT.) Length)

   Access to arrays

    1. Accessing a single element in an array initialized above

int i = mysigarray[2];        //i=3Int j = mymularray[2,1]; //j=8int k = mysawarray[2][//k=10 Note the difference between a two-dimensional array and a jagged array      

    2. Iterating through the array elements

C # provides a foreach statement that provides a simple, straightforward way to iterate through an array

foreach (in MyArray)      // 1,myarray The value of all array elements can be a one-dimensional, multidimensional, or jagged array {m++     

Alternatively, you can use the For loop to iterate through the array, which allows you to better control the array elements (knowing which array objects are in each loop), which is not possible with the foreach statement.

for (0; i< mysawarray.length; i++) {for    (0; J < Mysawarray[i]. Length; J + +// Assign each element of a jagged array to the corresponding element in a two-dimensional array }}      

  

Common properties and methods for arrays

  In C #, an array is actually an object. System.Array is an abstract base type for all array types, providing methods for creating, manipulating, searching, and sorting arrays, so that all arrays can use its properties and methods

1. Common Properties

    Length: Gets a 32-bit integer that represents the total number of elements in all the dimensions of the array, that is, the number of elements in the array, commonly used to limit the size of the array subscript

  

    2. Common methods

      2.1 Copying of arrays

      In C #, the replication of an array is generally divided into the following three types:

Int[] Pins = {3,9,7,2};int[] Copy1 =Newint[pins. Length];  pins. CopyTo (copy1,  0); // copy using the CopyTo () method of the array object, parameter 0 represents the first element from the Copy1 array (subscript is 0) Start placing values copied from the pins array int[] copy2 = int[pins. Length];  array.copy (pins,copy2,copy2. Length); //int[] Copy3 =  (int[]) pins. Clone (); // using the Clone () method of the Array object, it creates a new array, the return value is an object, So you need to force the type to be converted to the appropriate type               

      2.2 Sorting of arrays

      Sorting is one of the commonly used algorithms in programming. Two ways to sort arrays are provided in C #:

      Array.Sort (array) for small to large sorting of arrays

      Array.reverse (Array) for reverse sorting of arrays

Int[] Pins = {3,9,7,2};   Array.Sort (Pins); //// pins={9,7,3,2}

Array "Go" in C #

Related Article

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.