Introduction to arrays in C # (1)

Source: Internet
Author: User

An array is a data structure that contains several variables. These variables can be accessed through computing indexes. The elements of the array have the same type.
The array has a "rank ". The rank of an array is also called the dimension of an array. An array with the "rank" of 1 is called a one-dimensional array. An array with a "rank" greater than 1 is called a multi-dimensional array. A dimension array is usually called a two-dimensional array or a three-dimensional array.

Declare an array
When declaring an array, square brackets ([]) must follow the type, not the identifier. In C #, placing square brackets after identifiers is invalid syntax.

C # supports one-dimensional arrays, multi-dimensional arrays (rectangular arrays), and arrays (staggered arrays ).

One-dimensional array:

Int [] arrayname;

Multi-dimensional array:

Int [,] arrayname;

Array array (staggered ):

Int [] [] arrayname;

Declaring arrays does not actually create them. In C #, arrays are objects and must be instantiated.


Example:
Using System;

Class TestArray

{

Public static void Main ()

{

// Declare the reference of an integer one-dimensional array, and allocate space for five consecutive integer variables in the heap.

Int [] numbers = new int [5];

// Declare a reference to a two-dimensional string array

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

// Array, equivalent to declaring that the length of the one-dimensional array containing five byte-type one-dimensional arrays is 5

Byte [] [] scores = new byte [5] [];

// Instantiate each btye-type one-dimensional array

For (int I = 0; I <scores. Length; I ++)

{

Scores [I] = new byte [I + 3]; // non-rectangular

}

For (int I = 0; I <scores. Length; I ++)

{

Console. WriteLine ("Length of row {0} is {1}", I, scores [I]. Length );

}

}

}

Initialize an array
C # It provides a simple and straightforward method to initialize the array during declaration by including the initial value in braces. The following example shows how to initialize arrays of different types. Www.2cto.com

One-dimensional array
Int [] numbers = new int [5] {1, 2, 3, 4, 5 };
String [] names = new string [3] {"Matt", "Joanne", "Robert "};
The array size can be omitted, as shown below:

Int [] numbers = new int [] {1, 2, 3, 4, 5 };
String [] names = new string [] {"Matt", "Joanne", "Robert "};
If you provide an initial value setting item, you can also omit the new operator, as shown below:

Int [] numbers = {1, 2, 3, 4, 5 };
String [] names = {"Matt", "Joanne", "Robert "};
Multi-dimensional array
Int [,] numbers = new int [3, 2] {1, 2}, {3, 4}, {5, 6 }};
String [,] siblings = new string [2, 2] {"Mike", "Amy" },{ "Mary", "Albert "}};
The array size can be omitted, as shown below:

Int [,] numbers = new int [,] {1, 2}, {3, 4}, {5, 6 }};
String [,] siblings = new string [,] {"Mike", "Amy" },{ "Mary", "Albert "}};
If you provide an initial value setting item, you can also omit the new operator, as shown below:

Int [,] numbers = {1, 2}, {3, 4}, {5, 6 }};
String [,] siblings = {"Mike", "Amy" },{ "Mary", "Albert "}};
Staggered array (array)
You can initialize the staggered array as shown in the following example:

Int [] [] numbers = new int [2] [] {new int [] {2, 3, 4}, new int [] {5, 6, 7, 8, 9 }};
The size of the first array can be omitted, as shown below:

Int [] [] numbers = new int [] [] {new int [] {2, 3, 4}, new int [] {5, 6, 7, 8, 9 }};
-Or-

Int [] [] numbers = {new int [] {2, 3, 4}, new int [] {5, 6, 7, 8, 9 }};
Member Groups
The members of the member number can be directly performed, similar to those of the member number in C/C ++. For example, the following code creates an array named numbers and assigns 5 to the fifth element of the array:

Int [] numbers = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
Numbers [4] = 5;
The following Code declares a multi-dimensional array and assigns 5 to members located in [1, 1:

Int [,] numbers = {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10 }};
Numbers [1, 1] = 5;
The following declares a one-dimensional staggered array, which contains two elements. The first element is an array of two integers, and the second element is an array of three integers:

Int [] [] numbers = new int [] [] {new int [] {1, 2}, new int [] {3, 4, 5}
};
The following statement assigns 58 to the first element of the first array and 667 to the second element of the second array:

Numbers [0] [0] = 58;
Numbers [1] [1] = 667;
Use foreach for Arrays
C # also provides the foreach statement. This statement provides a simple and clear method to cyclically access the elements of the array. For example, the following code creates an array named numbers and cyclically accesses the Array Using the foreach statement:

Int [] numbers = {4, 5, 6, 1, 2, 3,-2,-1, 0 };
Foreach (int I in numbers)
{
System. Console. WriteLine (I );
}
With multidimensional arrays, you can use the same method to cyclically access elements. For example:

Int [,] numbers = new int [3, 2] {9, 99}, {3, 33}, {5, 55 }};
Foreach (int I in numbers)
{
Console. Write ("{0}", I );
}
The output of this example is:

9 99 3 33 5 55


From SYZ_YUMEIZHOU_YY

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.