【问题标题】:C# UWP Populating and accessing a nested listC# UWP 填充和访问嵌套列表
【发布时间】:2018-08-09 00:41:28
【问题描述】:

我正在创建一个存储和显示天气详细信息的应用程序。

在这个 sn-p result 是一个对象列表(40 个对象,5 天内每天 8 个天气预报),我将其分为天和小时。我正在使用嵌套列表来显示天列表中的每小时预测列表(每个 sortedDay 列表中都有自己的 sortedHours 列表。

当我打印输出时,每个对象都具有相同的值,并且每次打印后都会调用 Debug.WriteLine(" -");(39 次,但最多只能调用 5 次)

//create a list of weatherController lists to hold each day
List<List<WeatherController>> sortedDays =new List<List<WeatherController>>();

//create a list of weatherController objects to hold each hourly interval
List<WeatherController> sortedHours = new List<WeatherController>();

// a base time
DateTime prevDate = Convert.ToDateTime("2000-01-01");
int counter = 0;

// iterate through result list  
foreach (var wCount in result.list)
{
    // if the date is greater than the previous date add the sortedHours to sortedDays
    if (Convert.ToDateTime(result.list[counter].dt_txt) > prevDate && counter!=0)
    {
        sortedDays.Add(sortedHours);
        sortedHours.Clear();
    }
    WeatherController wController= new WeatherController();

    wController.dtime=result.list[counter].dt_txt;
    wController.temp = result.list[counter].main.temp;
    wController.humidity= result.list[counter].main.humidity;
    wController.desc = result.list[counter].weather[0].description;
    wController.windSpeed= result.list[counter].wind.speed;
    sortedHours.Add(wController);

    prevDate = Convert.ToDateTime(result.list[counter].dt_txt);
    counter++;

}

// test List of list Structure
int xCount=0,yCount=0;

foreach(var sd in sortedDays)
{
    foreach(var sh in sortedHours)
    {
        // DEBUG
        Debug.WriteLine(sortedDays[xCount][yCount].ToString());
        yCount++;
    }
    Debug.WriteLine(" -");
    xCount++;
    yCount = 0;
}

输出的一个sn-p:

dtime:2018-03-05 21:00:00 temp:274.687 humidity:100 desc:light rain windpeed:3.61

-
dtime:2018-03-05 21:00:00 temp:274.687 humidity:100 desc:light rain windpeed:3.61
-
dtime:2018-03-05 21:00:00 temp:274.687 humidity:100 desc:light rain windpeed:3.61
-
dtime:2018-03-05 21:00:00 temp:274.687 humidity:100 desc:light rain windpeed:3.61
-

【问题讨论】:

  • 您为什么使用foreach,然后不使用wCount(或sd,或sh)?您不妨只使用for 循环...
  • 由于数据是从api读取的,所以大小可能会发生变化,而且wCount是一个对象而不是整数值,也会被拆分成单独的方法
  • 我知道,我的意思是你可以使用wCount 而不是result.list[counter]。大小是result.list.Length(或.Count()),对吧……?
  • 哦,对了,我觉得这不是问题,因为当我调试它时,它会从result 获取正确的值并将它们正确分配给wController,但问题似乎出在何时它被添加到列表中
  • 对,这只是为了让您的代码更易于阅读而考虑的事情

标签: c# list object uwp nested-loops


【解决方案1】:

第一个问题是您为每个结果条目重复使用sortedHours 的一个实例。因为List&lt;T&gt; 是引用类型,所以变量指向内存中存储数据的位置。因为你 ClearAdd 在每次迭代中都指向同一个实例,所以在最后一个“回合”之后,它将只包含列表中的最后一项。

第二个问题是输入中的最后一项没有处理,因为foreach 将结束,sortedHours 列表将包含一些尚未分配给任何一天的项目。

终于日期比较有问题了:

Convert.ToDateTime(result.list[counter].dt_txt) > prevDate

不只比较日期。它比较日期和时间。这意味着 if 每次都会执行。要仅比较日期,您必须取消日期的时间部分,这可以通过使用 Date 属性最轻松地完成:

Convert.ToDateTime(result.list[counter].dt_txt).Date > prevDate.Date

【讨论】:

  • 好吧,有道理,我现在明白了,我可以调整它以每次创建一个新的 wController 实例,但至于创建一个新的 sortedHours 列表,我无法理解这是怎么回事将作为新列表实施?
  • 基本上,当您调用Add( sortedHours ) 时,您会将指向内存位置的指针添加到sortedDays 列表中。在下一行您调用 clear sortedHours - 这实际上清除了变量,但因为 sortedDays 列表中的项目指向同一个变量,它现在基本上指向一个空集合。如果您在 Clear 行之后放置一个断点,您可以很好地看到这一点 - 如果您将鼠标悬停在 sortedDays 变量上并调查添加的项目,您会看到它突然为空 - 那是因为它是相同的实例sortedHours :-)
  • 解决方案是使用sortedHours = new List&lt;WeatherController&gt;() 而不是Clear。这将创建一个新实例并保持已使用的实例完好无损。
  • 打个比方,假设您有一个指向硬盘上文件的快捷方式。如果您修改文件的内容,更改也会反映在快捷方式中,因为它指向的是同一个文件。
  • 问题已解决,知识得到了感谢您的所有帮助
猜你喜欢
  • 1970-01-01
  • 2013-03-16
  • 1970-01-01
  • 2020-12-10
  • 1970-01-01
  • 1970-01-01
  • 2019-01-01
  • 1970-01-01
相关资源
最近更新 更多