Get the index of the last element of the specified dimension in the array in CSharp
Description
The following code shows how to get the index of the last element of the specified dimension in the array.
Example
/* w w w . ja v a 2s . com*/
using System;
public class Example
{
public static void Main()
{
int[] integers = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
int upper = integers.GetUpperBound(0);
int lower = integers.GetLowerBound(0);
Console.WriteLine(lower);
Console.WriteLine(upper);
for (int ctr = lower; ctr <= upper; ctr++)
Console.WriteLine(integers[ctr]);
int[,] integers2d= { {2, 4}, {3, 9}, {4, 16}, {5, 25},
{6, 36}, {7, 49}, {8, 64}, {9, 81} };
int rank = integers2d.Rank;
Console.WriteLine("Number of dimensions: {0}", rank);
for (int ctr = 0; ctr < integers2d.Rank - 1; ctr++)
Console.WriteLine(" Dimension {0}: from {1} to {2}",
ctr, integers2d.GetLowerBound(ctr),
integers2d.GetUpperBound(ctr));
Console.WriteLine(" Values of array elements:");
for (int outer = integers2d.GetLowerBound(0); outer <= integers2d.GetUpperBound(0);outer++)
for (int inner = integers2d.GetLowerBound(1); inner <= integers2d.GetUpperBound(1);inner++){
Console.WriteLine(" {3}{0}, {1}{4} = {2}", outer, inner,integers2d.GetValue(outer, inner), "{", "}");
}
}
}
The code above generates the following result.