C # array declaration,

Source: Internet
Author: User

C # array declaration,

C # How to declare a one-dimensional array

Int [] myArray;

String [] myStrArr;

However, Initialization is required before accessing the array.

C # two methods are available for Array initialization. The first method is to assign an initial value to the element of the array when declaring the array:

Int [] myArray = {5, 9, 15, 22, 30 };
String [] myStrArr = {"digoal", "league", "brambling "};

Another method is to specify the array size when declaring an array (that is, the length of the array and the number of array elements ):

Int [] myArray = new int [5];
String [] myStrArr = new string [3];

Of course, it may not be a value or a variable with the keyword const:

Const int arrSize = 5;
Int [] myArray = new int [arrSize];
String [] myStrArr = new string [arrSize];

You can use the for loop to access the array element and the subscript corresponding to the array element:

Int [] myArray = {5, 9, 15, 22, 30 };
For (int I = 0; I <myArray. Length; I ++)
{
Console. WriteLine ("the array Length is {0}, the {1} element is: {2}", myArray. Length, I, myArray [I]);
}
Console. ReadKey ();

You can also use foreach to access each element of the array, but the foreach loop performs read-only access to the array content, so the value of any element cannot be changed:

Int [] myArray = {5, 9, 15, 22, 30 };
Foreach (int num in myArray)
{
Console. WriteLine ("array element: {0}", num );
}
Console. ReadKey ();

Two-dimensional array:

The simplest way to declare a two-dimensional array:

Int [,] myIntArray;
String [,] myStrArray;

A 4-dimensional array is declared as follows:

Int [,] myIntArray;

To declare a multi-dimensional array, you only need to add multiple commas based on the two-dimensional array.

Two-dimensional array initialization method:

Int [,] myIntArray = {1, 4, 7}, {2, 5, 8}, {3, 6, 9 }};
String [,] myStrArray = new string [3, 2];

Two-dimensional arrays access array elements by Subscript:

MyIntArray [1, 2]; // 8

MyIntArray is a two-dimensional array with three rows and three columns. Therefore, 1 in myIntArray [1, 2] indicates the second nested array of the array myIntArray (the logo under the array starts from 0, so 1 is the second element.) 2 in myIntArray [] refers to the third element in the second nested array of the array myIntArray, so its value is 8.

Multi-dimensional array:

Two declaration and initialization methods for multi-dimensional arrays:

Int [] [] myIntArrays = new int [2] [];
MyIntArrays [0] = new int [3];
MyIntArrays [1] = new int [4];

Another method:

Int [] [] myIntArrays = {new int [] {1, 2, 3}, new int [] {2, 3, 4, 5 }};

You can use the foreach loop to access elements of multi-dimensional arrays:

Int [] [] myIntArrays = {new int [] {1, 2, 3}, new int [] {2, 3, 4, 5 }};
Foreach (int [] numArr in myIntArrays)
{
Foreach (int num in numArr)
{
Console. WriteLine ("array element: {0}", num );
}
}

Output result:

Array element: 1
Array element: 2
Array element: 3
Array element: 2
Array element: 3
Array element: 4
Array element: 5

We can see the order of accessing multi-dimensional array elements.

The first time I posted a blog, I only started my blog in the last few days. The reason is that I want to record some things I have learned because I am too fond of cooking. I am very honored to be able to help others!

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.