开发者

How do you store calendar month and days in array?

开发者 https://www.devze.com 2022-12-31 08:36 出处:网络
How would you store all 12 months and their correct amount of days in a开发者_C百科n array? Making a 12 by 31 array is two much for some of the months, like Feb has only 28 days. The rows would be the

How would you store all 12 months and their correct amount of days in a开发者_C百科n array? Making a 12 by 31 array is two much for some of the months, like Feb has only 28 days. The rows would be the months and the columns would be the number of days. Suggestions?


I'm assuming you want one value per day, rather than just the number of days for each array...

You could either use a rectangular array and ignore the "extra" days or use a jagged array:

GregorianCalendar calendar = new GregorianCalendar();
string[][] values = new string[12][];
for (int i = 0; i < 12; i++)
{
    values[i] = new string[calendar.GetDaysInMonth(year, i + 1)];
}


Jagged arrays are the standard solution to your problem. Instead of

T[,] foo = new T[12,31];

use

T[][] foo = new T[12][];

foo[0] = new T[31];
foo[1] = new T[28];
foo[2] = new T[31];
foo[3] = new T[30];
       ...
foo[11] = new T[31];
0

精彩评论

暂无评论...
验证码 换一张
取 消