【问题标题】:Values are being overwritten with each key addition in dictionary字典中的每个键添加都会覆盖值
【发布时间】:2014-08-19 07:14:32
【问题描述】:
public Dictionary<string, List<int>> GetAllMemberIDsWithinATable()
{
    List<string> _sectionDataList = this.GetSectionInfo();
    var dict = new Dictionary<string, List<int>>();
    List<int> _memberIds = new List<int>();
    string _staadName = "";
    int iVal = 0;
    bool isEnd = false;

    for (int iSecList = 0; iSecList < _sectionDataList.Count(); iSecList++)
    {
        while ((iSecList < _sectionDataList.Count()) && 
               (!_sectionDataList[iSecList].Equals("TABLE")))
        {
            string data = _sectionDataList[iSecList];
            if (!(data.Equals("TO") || (data.Equals("-"))))
            {
                if (data.Equals("SP") || data.Equals("0.01"))
                {
                    isEnd = true;
                    break;
                }
                else if (!_memberIds.Contains(Convert.ToInt32(data)))
                    _memberIds.Add(Convert.ToInt32(data));
            }                    
            else
            {
                int jData = Convert.ToInt32(_sectionDataList[iSecList + 1]);
                if (iSecList != 0)
                    iVal = iSecList - 1;
                int xData = Convert.ToInt32(_sectionDataList[iVal]);
                for (int i = xData; i <= jData; i++)
                    if (!_memberIds.Contains(i))
                        _memberIds.Add(i);
            }
            iSecList++;
        }

        if (iSecList != _sectionDataList.Count()) 
        {
            if (!isEnd)
            {
                iSecList = iSecList + 2;
                _staadName = _sectionDataList[iSecList];
                this.staadName = _staadName;
                dict.Add(_staadName, _memberIds);
                _memberIds.Clear();
            }
        }
    }
    return dict;
}

_memberIds 被替换,而新密钥即_staadname 添加。就像我想将 TUB1501506、ISMC250 等作为我的密钥并从文件中获取它们并将数据存储在来自

memberIds

"43 至 62 71 72 82 92 101 至 105 110 至 112 137 至 146 156 157 168 171 173 - 174 185 至 188 197 198 218 220 至 222 240 241 244 256 272 274 275 - 280 至 282 285 至 290 294 台式 TUB1501506 63 至 69 73 至 77 79 至 81 83 至 87 89 至 91 93 至 100 108 109 113 至 117 - 119 至 136 189 至 195 206 209 至 215 217 223 225 至 239 245 253 264 278 291 - 293 297 表 FR ISMC250 1 至 4 7 至 22 25 至 28 31 至 42 147 至 155 158 至 166 175 至 183 292 - 299 表 FR ISMC200 SP 0.01"

如您所见,模式是最后一个字符串是_staadname,字符串之前给出的范围是该名称的memberIds 列表。

【问题讨论】:

    标签: c# dictionary


    【解决方案1】:

    这仅仅是因为您只有一个_memberIds 实例。您永远不会在循环内重新实例化它。因此,for 循环的每次迭代都会重用您的列表。

    尝试将其移动到循环内:

    // not here
    
    for (int iSecList = 0; iSecList < _sectionDataList.Count(); iSecList++)
    {
        // put it here
        List<int> _memberIds = new List<int>();
    
        // your original code
    }
    

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 2021-02-07
      • 2019-06-08
      • 2014-05-21
      • 2021-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多