C # array Learning

Source: Internet
Author: User
C # array Learning Array Overview

C #Index the array from scratch, that is, the array index starts from scratch.C #The working method of arrays in is similar to that in most other popular languages. However, some differences should be noted.

When declaring an array,Square brackets([])Must follow the type, not the identifier. InC #It is invalid to put square brackets behind the identifier.

Int [] Table; // not int table []; 

Another detail is,The array size is not part of its type.InCIt is part of the array type.. This allows you to declare an array and assign itIntAny array of the object regardless of the length of the array.

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

 Declare an array

C #Supports one-dimensional arrays, multi-dimensional arrays (rectangular arrays), and array arrays (staggered arrays ). The following example shows how to declare arrays of different types:

One-dimensional array:

 
Int [] numbers;

Multi-dimensional array:

 
String [,] names;

Array array (staggered ):

 
Byte [] [] scores;

Declaring arrays (as shown above) does not actually create them.InC #The array is an object.(This tutorial will be discussed later) and must be instantiated. The following example shows how to create an array:

One-dimensional array:

Int [] numbers = new int [5];

Multi-dimensional array:

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

Array array (staggered ):

 
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 a three-dimensional array of rectangles:

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

You can even mix a rectangle array with a staggered array. For exampleCodeDeclared typeInt A one-dimensional array of a two-dimensional array.

Int [] [,] [,] numbers;
 
 Initialize an array

C #Enclose the initial values in braces({})Provides a simple and straightforward method for initializing arrays during declaration. The following example shows how to initialize arrays of different types.

Note: If the array is not initialized at the time of declaration, the array members are automatically initialized to the default initial value of the array type. In addition, if an array is declared as a field of a certain type, it will be set to the default value when it is instantiated.Null.

 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, you can also omitNew OPERATOR:

 
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, you can also omitNew OPERATOR:

 
Int [,] numbers = {1, 2}, {3, 4}, {5, 6 }};
 
String [,] siblings = {"Mike", "Amy" },{ "Mary", "Albert "}};
Staggered array (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 }};

Note that there is no initialization syntax for elements in the staggered array.

Member Groups

Members of the worker group can directly perform the operation, similarC/C ++Group members. For example, the following code createsNumbers And then assign5:

 
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[1, 1] Assigned5:

 
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 assigns58To the second element of the second array667:

 
Numbers [0] [0] = 58;
 
Numbers [1] [1] = 667;
 
Array is an object

InC #Array is actually an object.System. Array Is the abstract base type of all array types. AvailableSystem. Array Attributes and other class members. An example of this usage is to use"Length"(Length) Attribute to obtain the length of the array. The following code willNumbers The length of the array (5) ToLengthofnumbers Variable:

 
Int [] numbers = {1, 2, 3, 4, 5 };
 
Int lengthofnumbers = numbers. length;

System. Array Class provides many useful other methods/Properties, such as the method used to sort, search, and copy arrays.

UseForeach

C #Also providesForeach Statement. This statement provides a simple and clear method to cyclically access the elements of the array. For example, the following code createsNumbers And useForeach Statement to access the array cyclically:

 
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

However, with multi-dimensional arrays, nestedFor Loop enables you to better control array elements.

Array Overview

C #Index the array from scratch, that is, the array index starts from scratch.C #The working method of arrays in is similar to that in most other popular languages. However, some differences should be noted.

When declaring an array,Square brackets([])Must follow the type, not the identifier. InC #It is invalid to put square brackets behind the identifier.

Int [] Table; // not int table []; 

Another detail is,The array size is not part of its type.InCIt is part of the array type.. This allows you to declare an array and assign itIntAny array of the object regardless of the length of the array.

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

 Declare an array

C #Supports one-dimensional arrays, multi-dimensional arrays (rectangular arrays), and array arrays (staggered arrays ). The following example shows how to declare arrays of different types:

One-dimensional array:

 
Int [] numbers;

Multi-dimensional array:

 
String [,] names;

Array array (staggered ):

 
Byte [] [] scores;

Declaring arrays (as shown above) does not actually create them.InC #The array is an object.(This tutorial will be discussed later) and must be instantiated. The following example shows how to create an array:

One-dimensional array:

 
Int [] numbers = new int [5];

Multi-dimensional array:

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

Array array (staggered ):

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 a three-dimensional array of rectangles:

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

You can even mix a rectangle array with a staggered array. For example, the following Code declares that the type isInt A one-dimensional array of a two-dimensional array.

 
Int [] [,] [,] numbers;
 
 Initialize an array

C #Enclose the initial values in braces({})Provides a simple and straightforward method for initializing arrays during declaration. The following example shows how to initialize arrays of different types.

Note: If the array is not initialized at the time of declaration, the array members are automatically initialized to the default initial value of the array type. In addition, if an array is declared as a field of a certain type, it will be set to the default value when it is instantiated.Null.

 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, you can also omitNew OPERATOR:

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, you can also omitNew OPERATOR:

 
Int [,] numbers = {1, 2}, {3, 4}, {5, 6 }};
String [,] siblings = {"Mike", "Amy" },{ "Mary", "Albert "}};
Staggered array (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 }};

Note that there is no initialization syntax for elements in the staggered array.

Member Groups

Members of the worker group can directly perform the operation, similarC/C ++Group members. For example, the following code createsNumbers And then assign5:

 
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[1, 1] Assigned5:

 
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 assigns58To the second element of the second array667:

 
Numbers [0] [0] = 58;
 
Numbers [1] [1] = 667;
 
Array is an object

InC #Array is actually an object.System. Array Is the abstract base type of all array types. AvailableSystem. Array Attributes and other class members. An example of this usage is to use"Length"(Length) Attribute to obtain the length of the array. The following code willNumbers The length of the array (5) ToLengthofnumbers Variable:

 
Int [] numbers = {1, 2, 3, 4, 5 };
 
Int lengthofnumbers = numbers. length;

System. Array Class provides many useful other methods/Properties, such as the method used to sort, search, and copy arrays.

UseForeach

C #Also providesForeach Statement. This statement provides a simple and clear method to cyclically access the elements of the array. For example, the following code createsNumbers And useForeach Statement to access the array cyclically:

 
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

However, with multi-dimensional arrays, nestedFor Loop enables you to better control 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.