АкушерствоАнатомияАнестезиологияВакцинопрофилактикаВалеологияВетеринарияГигиенаЗаболеванияИммунологияКардиологияНеврологияНефрологияОнкологияОториноларингологияОфтальмологияПаразитологияПедиатрияПервая помощьПсихиатрияПульмонологияРеанимацияРевматологияСтоматологияТерапияТоксикологияТравматологияУрологияФармакологияФармацевтикаФизиотерапияФтизиатрияХирургияЭндокринологияЭпидемиология
|
A multidimensional array is indexed by two or more integers. Declaring this two - dimensional array with C# is done by putting a comma inside the brackets
indexer:
<nt[,] twodim = new int[3, 3];
Or
int[,] twodim = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
twodim[0, 0] = 1;
By using two commas inside the brackets, you can declare a three - dimensional array:
int[,,] threedim = {
{ { 1, 2 }, { 3, 4 } },
{ { 5, 6 }, { 7, 8 } },
{ { 9, 10 }, { 11, 12 } }
};
Console.WriteLine(threedim[0, 1, 1]);
twodim[0, 1] = 2;
twodim[0, 2] = 3;
twodim[1, 0] = 4;
twodim[1, 1] = 5;
twodim[1, 2] = 6;
twodim[2, 0] = 7;
twodim[2, 1] = 8;
twodim[2, 2] = 9;
Jagged arrays
A two - dimensional array has a rectangular size (for example, 3 3 elements). A jagged array is more flexible in sizing the array. With a jagged array every row can have adifferent size.
| int[][] jagged = new int[3][];
jagged[0] = new int[2] { 1, 2 };
jagged[1] = new int[6] { 3, 4, 5, 6, 7, 8 };
jagged[2] = new int[3] { 9, 10, 11 };
|
Array class in C#.
Def: Features of array class
-Length property returns the number of elements in the array. If array is multidimensional, then you get the number of elements in all measurements. If you need to know the number of elements within the dimension you can be used instead of the method GetLength (). Desc:
-LongLength -Length property returns a value of type int, and the property LongLength – length as the value of long. -Rank property allows you to get the number of dimensions of the array.
The Array class is abstract, so you cannot create an array by using a constructor. It is also possible to create arrays by using the static CreateInstance() method. This is useful if you don’t know the type of elements in advance. You can set values with the SetValue() method and read values with the GetValue() method.
Array intArray1 = Array.CreateInstance(typeof(int), 5);
for (int i = 0; i < 5; i++)
{intArray1.SetValue(33, i);}
for (int i = 0; i < 5; i++)
{Console.WriteLine(intArray1.GetValue(i));}
You can also cast the created array to an array declared as int[]: int[] intArray2 = (int[])intArray1;
The CreateInstance() method has many overloads to create multidimensional arrays and also to create arrays that are not 0-based. The following example creates a two-dimensional array with 2 3 elements.The first dimension is 1-based; the second dimension is 10-based.
int[] lengths = { 2, 3 };
int[] lowerBounds = { 1, 10 };
Array racers = Array.CreateInstance(typeof(Person), lengths, lowerBounds);
Setting the elements of the array, the SetValue() method accepts indices for every dimension:
Дата добавления: 2015-09-18 | Просмотры: 657 | Нарушение авторских прав
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
|