C # Array Learning related data collation _c# tutorial

Source: Internet
Author: User
Tags arrays
Array Overview

The C # array is indexed from zero, that is, the array index is zero-based. Arrays in C # work in a similar way to how they work in most other popular languages. But there are some differences that should be noticed.
When declaring an array, The square brackets ([]) must follow the type, not the identifier, after。 In C #, placing square brackets after an identifier is an illegal syntax.

Int[] table; not int table[];

Another detail is that the size of an array is not part of its type, but it is part of the array type in C language. This allows you to declare an array and assign it to any array of int objects, regardless of the length of the array.
Copy Code code as follows:

Int[] numbers; Declare numbers as an int array of any size
Numbers = new INT[10]; Numbers is a 10-element array
Numbers = new INT[20]; Now it ' s a 20-element array


declaring an array

C # supports arrays of one-dimensional, multidimensional (rectangular) arrays (jagged arrays). The following example shows how to declare an array of different types:

One-dimensional arrays:

Int[] Numbers multidimensional array:

string[,] names array of arrays (interleaved):

Byte[][] Scores declaration arrays, as shown above, do not actually create them. In C #, Arrays are objects(discussed later in this tutorial), it must be instantiated. The following example shows how to create an array:

one-dimensional arrays:

int[] numbers = new INT[5];
Multidimensional Arrays:
string[,] names = new string[5,4];
Array of Arrays (interleaved):
Copy Code code as follows:

Byte[][] scores = new byte[5][];
for (int x = 0; x < scores. Length; X + +)
{
SCORES[X] = new BYTE[4];
}

You can also have a larger array. For example, you can have three-dimensional rectangular arrays:
Copy Code code as follows:

Int[,,] buttons = new int[4,5,3];

You can even mix a rectangular array with a jagged array. For example, the following code declares a one-dimensional array of three-dimensional arrays of two-dimensional arrays of type int.

int[][,,][,] numbers;
Initializing array C # provides a simple and straightforward way to initialize an array at declaration time by enclosing the initial value in braces ({}). The following example shows various methods for initializing different types of arrays.

Attention If an array is not initialized at declaration time, the array member is automatically initialized to the default initial value of the array type. Also, if you declare an array as a field of a type, it will be set to the default value NULL when the type is instantiated。

One-dimensional arrays
Copy Code code as follows:

int[] numbers = new Int[5] {1, 2, 3, 4, 5};
string[] names = new String[3] {"Matt", "Joanne", "Robert"};

You can omit the size of the array, as follows:
Copy Code code as follows:

int[] numbers = new int[] {1, 2, 3, 4, 5};
string[] names = new string[] {"Matt", "Joanne", "Robert"};

If you provide an initializer, you can also omit the new operator, as follows:
Copy Code code as follows:

int[] numbers = {1, 2, 3, 4, 5};
String[] names = {"Matt", "Joanne", "Robert"};

Multidimensional arrays
Copy Code code as follows:

int[,] numbers = new int[3, 2] {1, 2}, {3, 4}, {5, 6}};
string[,] siblings = new string[2, 2] {{"Mike", "Amy"}, {"Mary", "Albert"}};

You can omit the size of the array, as follows:
Copy Code code as follows:

int[,] numbers = new int[,] {{1, 2}, {3, 4}, {5, 6}};
string[,] siblings = new string[,] {{"Mike", "Amy"}, {"Mary", "Albert"}};

If you provide an initializer, you can also omit the new operator, as follows:
Copy Code code as follows:

int[,] numbers = {1, 2}, {3, 4}, {5, 6}};
string[,] siblings = {{"Mike", "Amy"}, {"Mary", "Albert"}};

Jagged Array (array of arrays)
You can initialize a jagged array as shown in the following example:
int[][] numbers = new int[2][] {new int[] {2,3,4}, new int[] {5,6,7,8,9}};
You can omit the size of the first array, as follows:
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}};
Note that there is no initialization syntax for elements of a jagged array.

Accessing Array members

Accessing an array member can be done directly, similar to accessing an array member in C + +. For example, the following code creates an array named numbers, and then assigns a 5 to the fifth element of the array:
Copy Code code as follows:

int[] numbers = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
NUMBERS[4] = 5;

The following code declares a multidimensional array and assigns 5 to members at [1, 1]:
Copy Code code as follows:

int[,] numbers = {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}};
Numbers[1, 1] = 5;

The following declares a one-dimensional jagged array that 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 a 58 to the first element of the first array and assigns 667 to the second element of the second array:
Copy Code code as follows:

Numbers[0][0] = 58;
NUMBERS[1][1] = 667;


Arrays are objects
In C #, an array is actually an object. System.Array is the abstract base type for all array types. You can use the properties that System.Array has, as well as other class members. An example of this usage is to use the length property to get the length of the array. The following code assigns the length of the numbers array (5) to the variable named Lengthofnumbers:
Copy Code code as follows:

int[] numbers = {1, 2, 3, 4, 5};
int lengthofnumbers = numbers. Length;

The System.Array class provides many other methods/properties that are useful, such as methods for sorting, searching, and copying arrays.

use foreach for arrays

C # also provides a foreach statement. This statement provides a simple, straightforward way to iterate through the elements of an array. For example, the following code creates an array named numbers and iterates through the array with a foreach statement:
Copy Code code as follows:

int[] Numbers = {4, 5, 6, 1, 2, 3,-2,-1, 0};
foreach (int i in numbers)
{
System.Console.WriteLine (i);
}

Because of multidimensional arrays, you can use the same method to iterate through the elements, for example:
Copy Code code as follows:

int[,] numbers = new int[3, 2] {9, 99}, {3, 33}, {5, 55}};
foreach (int i in numbers)
{
Console.Write ("{0}", i);
}

The output of the example is:

9 99 3 33 5 55
However, with multidimensional arrays, using a nested for loop gives you better control over the array elements.

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.