Error[8]: Trying to access array offset on value of type null, File: /www/wwwroot/outofmemory.cn/tmp/model_misc.func.php, Line: 1274
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 190, wqo_prev_next_read(1230857)
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Trying to access array offset on value of type null, File: /www/wwwroot/outofmemory.cn/tmp/model_misc.func.php, Line: 1275
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 190, wqo_prev_next_read(1230857)
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
c# – 如果列表包含多个连续月份条带,则返回true_C_内存溢出

c# – 如果列表包含多个连续月份条带,则返回true

c# – 如果列表包含多个连续月份条带,则返回true,第1张

概述我正在使用一个类型为DateTime的C#列表,它有几行dd / mm / yyyy格式的数据,我试图找出一个方法,如果以下三个条件之一为真,则返回true >如果所有列表项都相同,即.相同的月份和年份很简单 >如果按月和按年顺序排列. 例: 10/4/2016 10/3/2016 10/2/2016 10/1/2016 10/12/2015 在上面,Month和Year按顺 我正在使用一个类型为DateTime的C#列表,它有几行dd / mm / yyyy格式的数据,我试图找出一个方法,如果以下三个条件之一为真,则返回true

>如果所有列表项都相同,即.相同的月份和年份很简单
>如果按月和按年顺序排列.

例:

10/4/2016  10/3/2016   10/2/2016   10/1/2016   10/12/2015

在上面,Month和Year按顺序排列,因此该方法返回true.

3.如果列表按顺序有多个月份

例:

10/2/2016   10/1/2016   10/12/201510/2/2016   10/1/2016   10/12/201510/2/2016   10/1/2016   10/12/2015

在上面的列表中,它有三个连续月份12 / 2015,1 / 2016,2 / 2016.因此,对于上面的列表,它应该返回true.即使一个月不按顺序,它也应该返回false

我能够为第二个条件编写一个方法,其中dd / yyyy应该按顺序使用以下方法

for (var x = 1; x < terms.Count; ++x)    {        var d1 = terms[x - 1];        var d2 = terms[x];        if (d2.Year == d1.Year)        {            if ((d1.Month - d2.Month) != 1) return false;            continue;        }                                      if ((d1.Year - d2.Year) != 1) return false;        if (d1.Month != 1 || d2.Month != 12) return false;    }    return true;

有没有类似的方法来检查第三个条件?

解决方法 我不会使用月份和年份,但考虑增加时间.

DateTime currentDateTime;foreach(var d in terms){  if(currentDateTime == default(DateTime))  {    currentDateTime = d;  }  else  {    currentDateTime = currentDateTime.AddMonths(1);    if(currentDateTime != d) return false;  }}return true;

对于重复相同的序列(这次是伪代码):

// Determine initial sequencewhile(currentDateTime.AddMonths(1) == nextDateTime) count++;// Make sure that the whole sequence is a multiple of the short sequenceif(totalLength % count != 0) return false;// Check remaining sequencesfor(iteration in 1 .. totalLength / count)  for(index in 1 .. count)    if(terms[index] != terms[count * iteration + index]) return false;// No elements differedreturn true;
总结

以上是内存溢出为你收集整理的c# – 如果列表包含多个连续月份条带,则返回true全部内容,希望文章能够帮你解决c# – 如果列表包含多个连续月份条带,则返回true所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1230857.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫

发表评论

登录后才能评论

评论列表(0条)

保存