Array of C,

Source: Internet
Author: User

Array of C,

What is an array?
An array is a data structure that contains multiple elements of the same type.
Array declaration:
Int [] myIntArray;
Note:When declaring an array, square brackets ([]) must follow the type instead of the variable name. In C #It is invalid to put square brackets after the variable name..
Array initialization:

We know that the array is of the reference type, so we need to allocate memory to the array.
1. myIntArray = new int [3];
2. myIntArray = new int [] {1, 2, 3 };
3. int [] myIntArray = {1, 2, 3 };// When this method is used to initialize an array, it can only be used to declare an array of variables. It cannot be used after the array is declared.
Array access:
After the array is declared and initialized, you can use the indexer for access. The indexer always starts with 0, indicating the first element.

int[] myIntArray = { 1, 2, 3 };Console.WriteLine("intValue = {0}", myIntArray[0]);Console.Read();

Result: intValue = 1
Array type:
1. Multi-dimensional array:
An array (also called a one-dimensional array) is indexed by an integer. A multi-dimensional array is indexed by two or more integers.

Static void Main (string [] args) {// declare a two-dimensional array with two rows and three columns: int [,] myIntArray1; myIntArray1 = new int [2, 3]; myIntArray1 [0, 0] = 1; myIntArray1 [0, 1] = 11; myIntArray1 [0, 2] = 111; myIntArray1 [1, 0] = 2; myIntArray1 [1, 1] = 22; myIntArray1 [1, 2] = 222; // equivalent to int [,] myIntArray1 ={{ 111, 222}, {,}; Console. writeLine ("{0}, {1}, {2}", myIntArray1 [0, 0], myIntArray1 [0, 1], myIntArray1 [0, 2]); Console. writeLine ("{0}, {1}, {2}", myIntArray1 [1, 0], myIntArray1 [1, 1], myIntArray1 [1, 2]); Console. read ();}

Result:

Static void Main (string [] args) {// declare a two-dimensional array with three rows and three columns int [,] myIntArray2; myIntArray2 = new int [,] {1, 11,111 }, {2, 22,222}, {3, 33,333}, {4, 44,444}; Console. writeLine ("{0}, {1}, {2}", myIntArray2 [0, 0], myIntArray2 [0, 1], myIntArray2 [0, 2]); Console. writeLine ("{0}, {1}, {2}", myIntArray2 [1, 0], myIntArray2 [1, 1], myIntArray2 [1, 2]); Console. writeLine ("{0}, {1}, {2}", myIntArray2 [2, 0], myIntArray2 [2, 1], myIntArray2 [2, 2]); Console. writeLine ("{0}, {1}, {2}", myIntArray2 [3, 0], myIntArray2 [3, 1], myIntArray2 [3, 2]); Console. read ();}

Result:

Static void Main (string [] args) {// declare a three-dimensional array with three columns: int [,] myIntArray3; myIntArray3 = new int }, {11,11 },{ 111,111 }},{ {222,222}, {333,333}, {}},{ 3, 3}, {3, 33 }}, {444,444}, {}, {}}; Console. writeLine ("{0}, {1}, {2}, {3}, {4}, {5}", myIntArray3 [0, 0, 0], myIntArray3 [0, 0, 1], myIntArray3 [0, 1, 0], myIntArray3 [0, 1, 1], myIntArray3 [0, 2, 0], myIntArray3 [0, 2, 1]); Console. writeLine ("{0}, {1}, {2}, {3}, {4}, {5}", myIntArray3 [1, 0, 0], myIntArray3 [1, 0, 1], myIntArray3 [1, 1, 0], myIntArray3 [1, 1, 1], myIntArray3 [1, 2, 0], myIntArray3 [1, 2, 1]); Console. writeLine ("{0}, {1}, {2}, {3}, {4}, {5}", myIntArray3 [2, 0, 0], myIntArray3 [2, 0, 1], myIntArray3 [2, 1, 0], myIntArray3 [2, 1, 1], myIntArray3 [2, 2, 0], myIntArray3 [2, 2, 1]); Console. writeLine ("{0}, {1}, {2}, {3}, {4}, {5}", myIntArray3 [3, 0, 0], myIntArray3 [3, 0, 1], myIntArray3 [3, 1, 0], myIntArray3 [3, 1, 1], myIntArray3 [3, 2, 0], myIntArray3 [3, 2, 1]); Console. read ();}

Result:

2. Sawtooth array:
Generally, the size of a multi-dimensional array is rectangular, while the size of a sawtooth array is flexible. Each row can have different sizes.

When initializing a sawtooth array, set the number of rows contained in the array. The second bracket that defines the number of elements in each row is set to null, because each row of this array contains different numbers of elements.

Static void Main (string [] args) {// declare a three-line int [] [] myIntArray4; myIntArray4 = new int [3] []; myIntArray4 [0] = new int [] {111,}; myIntArray4 [1] = new int [2] {2, 22 }; myIntArray4 [2] = new int [] {3,33, 333,3333}; for (int I = 0; I <myIntArray4.Length; I ++) {for (int j = 0; j <myIntArray4 [I]. length; j ++) {Console. writeLine ("{0}", myIntArray4 [I] [j]) ;}} Console. read ();}

Result:

 

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.