The----of the Sunwen Tutorial C # advanced 3

Source: Internet
Author: User
Tags arrays
Tutorial Sunwen Tutorial----C # Advanced
Three
Mrfat@china.com
Finally another day, get up, stretch, and sit in front of the computer. What I'm going to tell you today is an array of C # (Arrays). The array in C # is the same as some other excellent languages, it's also starting at 0, which we can see from our previous examples, that is, the first element of an array is a[0], rather than a (1) like VB. Although so, you should pay attention to some differences.

When declaring an array, the brackets must follow the type, not following the variable name, such as:
Int[] table; cannot be written as int table[]
This is obviously different from Java, which is possible in Java.

And in C # you can not specify the size of the array, which is not the same as the C language. This allows you to specify an array of any length, as follows:
Int[] numbers; Its length is arbitrary.
Of course, you can also specify its size:
INT[10] numbers;//Specifies an array of length 10.

In C #, the supported arrays include: single-dimension arrays, multidimensional arrays, and multiple arrays. They are declared in the following ways:
Single-dimension array:
Int[] numbers;
Multidimensional Arrays:
string[,] names;
Multiple arrays:
Byte[][] scores;


Declaring an array does not mean that it has been established. In C #, all the array elements are objects (pour! How to speak like Java &*%$#@), so before you build it, you first instantiate it:
Single-dimension array:
int[] numbers = new INT[5];
Multidimensional Arrays:
string[,] names = new string[5,4];
Multiple arrays:


Byte[][] scores = new byte[5][];
for (int x = 0; x < scores. Length; X + +)
{
SCORES[X] = new BYTE[4];
}
Oh, this is a little strange, first ignore it, later.

We can also create larger arrays, such as a three-dimensional array:
Int[,,] buttons = new int[4,5,3];


We can even mix multidimensional and multiple arrays, and the following examples illustrate these:
int[][,,][,] numbers;

The following example shows all of the above methods for building arrays:


M://Arrays\arrays.cs
001:using System;
002:class Declarearrayssample
003: {
004:public static void Main ()
005: {
006://single-dimensional Array
007:int[] numbers = new INT[5];
008:
009://multidimensional array
010:string[,] names = new string[5,4];
011:
012://Array-of-arrays (jagged Array)
013:byte[][] scores = new byte[5][];
014:
015://Create the jagged array
016:for (int i = 0; I < scores. Length; i++)
017: {
018:scores[i] = new BYTE[I+3];
019:}
020:
021://Print length of each row
022:for (int i = 0; I < scores. Length; i++)
023: {
024:console.writeline ("Length of row {0} is {1}", I, scores[i]. Length);
025:}
026:}
027:}
Its output is:

Length of row 0 is 3
Length of row 1 is 4
Length of row 2 is 5
Length of row 3 is 6
Length of row 4 is 7
The initialization of an array in C # can be initialized when it is established. As with Java and C, use {}. Of course, obviously, your initialization value must be the same as the array type you declared, for example, if you define an int type, you can't give it a string, alas, Java is much, in C #, String should be written as string, or else there would be an error. Sunwen may have such a mistake in the course that follows, and I hope you will correct me. hehe!

The following example illustrates the initialization of an array:

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


You can also omit the size of the array, such as:

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

You can even omit the new name if you give the value:


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

In C #, array access is the same as C/c++/java, the following statement creates an array and assigns its fifth element to 5:
int[] numbers = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
NUMBERS[4] = 5;

If you don't have c/java/c++ programming experience, then sunwen this reminder that numbers[4] represents the fifth element of the array, because I said earlier that the array starts at 0, so the 0,1,2,3,4 is exactly fifth, so .... (Under the table: stupid, you think we don't know ah, go on to say!)

The following example is about multidimensional arrays:

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

Again, all of the arrays in C # are objects (Faint,d version), so you can access the array by accessing the object's methods. And System.Array is the abstraction of the array. You can see the documentation for the methods supported by the array class. For instance, You can use the Length property to access the lengths of the array. The following example:

int[] numbers = {1, 2, 3, 4, 5};
int lengthofnumbers = numbers. Length;
Oh, well, another lesson, now is Beijing time 9:16 A.M., I want to have a rest! Haha, see you later!
Next page


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.