C # Array Instance introduction (GRAPHIC)

Source: Internet
Author: User

An array is a group of identical types that are grouped together, using a generic name, to access the elements in the data collection by assigning the subscript.

An array is a set of data that has the same type. When you access data in an array, you can specify it by subscript. Array elements in C # can be any data type, array subscript starts with 0, that is, the first element corresponds to the subscript 0, and then increments one after the other. Arrays can be one-dimensional or multidimensional.

One-dimensional arrays are the most basic types of arrays, and they are declared as follows:

data type [] array name;

Example:

int [] Anarray; Declares a one-dimensional array of integral types

An array with two dimensions is a two-dimensional array that is declared as follows:

data type [,] array name;

Example:

int [,] anarray; Declares a two-dimensional array of integral types

float [,]anarrayoffloats; Declaring a two-dimensional array of floating-point types

string [,] anarrayofstrings; Declares a two-dimensional array of string literals

When declaring an array variable, an array is not created yet, and no memory space is allocated for the elements in the array, so after declaring the arrays, you need to instantiate them:

Anarray = new int [2,4];

anarrayofstrings = new stirng [2,4];

We can also initialize the array element with the given value.

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

string [,] anarrayofstrings = new string [2, 2] {{"tithing", "Person B"}, {"Champion", "runner-Up"}};

You can also use the following shortcuts:

int [,] Anarray = {{0,1,2,3},{1,2,3,4}};

string [,] anarrayofstrings = {{"tithing", "Person B"}, {"Champion", "runner-Up"}};

In the C # language, arrays provide us with some useful features that allow us to accomplish some of the more advanced features.

The array name. Length: Returns an integer that represents the total number of elements in all the dimensions of the array.

The array name. Rank: Returns an integer that represents the number of dimensions of the array.

The array name. GetLength (int dimension): Returns an integer that represents the number of elements in the specified dimension of the array (specified by the parameter dimension, zero-based on the dimension).


The 4.foreach statement loops through the embedded statement for each element in the array or collection.


The syntax format for a foreach statement is:

foreach (data type identifier in expression)

Embed statement

An array of one-dimensional integers containing 6 elements;

Int[] Mf1=new int[6]; Note the scope of the initialized array, or specify the initial value;

An array of one-dimensional integers containing 6 elements, initial 1,2,3,4,5,6

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

A one-dimensional array of strings that can also omit the new operator if an initializer is provided

String[] mf3={"C", "C + +", "C #"};

One-dimensional object array

object[] Mf4 = new Object[5] {26, 27, 28, 29, 30};

Two-dimensional integer array, initial value mf5[0,0]=1,mf5[0,1]=2,mf5[1,0]=3,mf5[1,1]=4

int[,] mf5=new int[,]{{1,2},{3,4}};

6*6 array of two-dimensional integers

int[,] mf6=new mf[6,6];

Let's look at the traversal of a one-dimensional string array

Program Result: Arr[0]=c arr[1]=c++ arr[2]=c#

Let's look at a traversal of an integer array of 4 rows and 2 columns (4*2):


Operating result: arr[0,0]=1 arr[0,1]=2 arr[1,0]=3 arr[1,1]=4 arr[2,0]=5 arr[2,1]=6 arr[3,0]=7 arr[3,1]=8


Properties of the array:

? The array can be one-dimensional, multidimensional, or interleaved.

The default value of a numeric array element is set to zero, and the default value of the reference element is set to NULL.

The jagged array is an array of arrays, so its elements are reference types and are initialized to null.

The index of the array is zero-based: an array with n elements is indexed from 0 to n-1.

? An array element can be any type, including an array type.

? The array type is a reference type derived from the abstract base type array. Because this type implements IEnumerable and IEnumerable, you can use foreach iterations for all arrays in C #.

My understanding of the array: in C #, arrays are actually objects, not just addressable contiguous memory regions like C and C + +

One-dimensional arrays: a one-dimensional array stores a fixed number of items in a linear fashion, with an index value that identifies any one item.

One-dimensional array instances:

Static class program  {    static void Main ()    {      //one-dimensional array      int[] Arry = new Int[9] {1, 2, 3, 4, 5, 6, 7, 8, 9 };      Arry[0] = 2;//First act 2      Console.WriteLine (Arry);}  }

Run results



Two-dimensional array instances

Two-dimensional array      int[,] arry2 = new int[2, 3] {{1, 2, 3}, {4, 5, 6}};      arry2[1, 0] = 5;//changes the first behavior of the second column 5 for      (int i = 0; i < Arry2. GetLength (1); i++)      {for        (int y = 0; y < Arry2. GetLength (1); y++)//The first for loop iterates through the columns of the two-dimensional array, and the second for loop traverses the row        {          Console.Write (arry2[i, y]) of the two-dimensional array;}      }

Run results


Jagged Array Instances

Jagged array     int[][] arry3 = new int[3][];//jagged array must specify the number of subscripts     arry3[0] = new int[] {0, 1, 2, 4};     ARRY3[1] = new int[] {3, 4, 5, 6};     ARRY3[2] = new int[] {4, 8, 9};     for (int x = 0; x < Arry3. Length; x + +)     {for       (int z = 0; z < arry3[x]. Length; z++)       {         console.write (arry3[x][z]);       }       Console.WriteLine ();//Line wrapping as Inti array element.     }

Run results


C # also provides a foreach statement. This statement provides a simple, straightforward way to iterate through the elements of an array, as shown in the following example.

static void Main ()    {int [] arry4=new int[100];      foreach (int a in Arry)      {        console.write (arry);      }}

Run results

Source:

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
C # Array Instance introduction (GRAPHIC)

This address: http://www.paobuke.com/develop/c-develop/pbk23574.html






Related content C #? Ù? Y±í???? Êy?¢?? Êy?ó?? 2?í??? E? C # implements access universal Access class Oledbhelper Complete example of the use of a singleton pattern in design Patterns in C # programming C # Message time formatting
C # Implementation of the most complete file and directory Operations class instance analysis of the difference between sleep and wait in C # methods for reading, writing, creating, copying, moving, and deleting files in C # WinForm ListBox and ComboBox bound data usage instances

C # Array Instance introduction (GRAPHIC)

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.