C#数组问题
C#中可否定义数组比如a[],让其从a[1]开始,而不是从a[0]开始?
还有,能否定义一个从1(而不是0)开始加1循环的整数?
------解决方案--------------------C#中可否定义数组比如a[],让其从a[1]开始,而不是从a[0]开始?
--------
你不用第0个元素不就得了
还有,能否定义一个从1(而不是0)开始加1循环的整数?
-------
你想从几开始循环就从几开始
------解决方案--------------------using System;
public sealed class DynamicArrays {
public static void Main() {
// I want a 2-dimensional array [2005..2009][1..4].
Int32[] lowerBounds = { 2005, 1 };
Int32[] lengths = { 5, 4 };
Decimal[,] quarterlyRevenue = (Decimal[,])
Array.Createlnstance(typeof(Decimal), lengths, lowerBounds);
Console.WriteLine( "{0,4} {1,9} {2,9} {3,9} {4,9} ",
"Year ", "Q1 ", "Q2 ", "Q3 ", "Q4 ");
Int32 firstYear = quarterlyRevenue.GetLowerBound(0);
Int32 lastYear = quarterlyRevenue.GetUpperBound(0);
Int32 firstQuarter = quarterlyRevenue.GetLowerBound(1);
Int32 lastQuarter = quarterlyRevenue.GetUpperBound(1);
for (Int32 year = firstYear; year <= lastYear; year++) {
Console.Write(year + " ") ;
for (Int32 quarter = firstQuarter; quarter <= lastQuarter; quarter++) {
Console.Write( "{0,9:c} ", quarterlyRevenue[year, quarter]);
}
Console.WriteLine();
}
}
}