Learn to use C multidimensional arrays
- 2020-05-07 20:15:08
- OfStack
String[, ,] items =
new String[,,] {
{
{ "A1", "A2", "A3", " Do things ", " a. " },
{ "B1", "B2", "B3", " Do things ", " a. " },
{ "C1", "C2", "C3", " Do things ", " a. " },
{ "D1", "D2", "D3", " Do things ", " a. " }
}, {
{ "E1", "E2", "E3", " Do things ", " a. " },
{ "F1", "F2", "F3", " Do things ", " a. " },
{ "G1", "G2", "G3", " Do things ", " a. " },
{ "H1", "H2", "H3", " Do things ", " a. " }
}
};
Code:
System.Console.WriteLine("Items.Rank =" + items.Rank);
System.Console.WriteLine("Items.GetUpperBound(0)=" + items.GetUpperBound(0));
System.Console.WriteLine("Items.GetUpperBound(1)=" + items.GetUpperBound(1));
System.Console.WriteLine("Items.GetUpperBound(2)=" + thirdItems.GetUpperBound(items.Rank - 1));
System.Console.WriteLine("Items[0, 0, 0]=" + items[0, 0, 0]);
System.Console.WriteLine("Items[0, 0, 1]=" + items[0, 0, 1]);
System.Console.WriteLine("Items[0, 0, 2]=" + items[0, 0, 2]);
System.Console.WriteLine("Items[0, 0, 3]=" + items[0, 0, 3]);
System.Console.WriteLine("Items[0, 0, 4]=" + items[0, 0, 4]);
System.Console.WriteLine("Items[0, 1, 0]=" + items[0, 1, 0]);
System.Console.WriteLine("Items[0, 2, 0]=" + items[0, 1, 1]);
System.Console.WriteLine("Items[0, 2, 0]=" + items[0, 1, 2]);
System.Console.WriteLine("Items[0, 2, 0]=" + items[0, 1, 3]);
System.Console.WriteLine("Items[0, 2, 0]=" + items[0, 1, 4]);
The operation results are as follows:
Items.Rank =3
Items.GetUpperBound(0)=1
Items.GetUpperBound(1)=3
Items.GetUpperBound(2)=4
Items[0, 0, 0]=A1
Items[0, 0, 1]=A2
Items[0, 0, 2]=A3
Items[0, 0, 3]= Do things
Items[0, 0, 4]= a.
Items[0, 1, 0]=B1
Items[0, 2, 0]=B2
Items[0, 2, 0]=B3
Items[0, 2, 0]= Do things
Items[0, 2, 0]= a.
Among them:
GetUpperBound(0) returns the upper limit of the index of the first dimension of the array, GetUpperBound(i) returns the upper limit of the i+1 dimension of the array, and GetUpperBound(Rank-1) returns the upper limit of the last one dimension of the array, which is the number of columns minus 1