C#字典(dictionary)中的旧value被新value替换了

.Net技术 码拜 7年前 (2017-04-26) 2143次浏览
以下代码中第49行,Storage这个类中的字典的旧value被新value取代,但是key没有被取代,求指导

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo dir = new DirectoryInfo(".");//new DirectoryInfo(".") 
            FileInfo[] inf = dir.GetFiles();
            StreamReader rd;//用于打开“A5E”文件
            Storage stor = new Storage();//实例化
            foreach (FileInfo finf in inf)
            {
                if (finf.Extension.Equals(".A5E")) //假如扩展名为“.A5E”
                {
                    string path = finf.Name;
                    rd = File.OpenText(path);
                    string s = rd.ReadToEnd();
                    string[] sArray = s.Split("&"); // split the string into seperate strings using &
                    Storage.counter = 0;
                    for (int i = 0; i < sArray.Length; i++)//i是区分按照& split分开的部分
                    {
                        if (i % 2 == 1)
                        {
                            string[] lineArray = sArray[i].Split("\n"); //利用正则表达式将字符串中的数字提取出来分别保存在小数数组中                          
                            for (int ii = 7; ii < (lineArray.Length - 3); ii++) //ii是为了区分everyline of reinforcement layers
                            {
                                Storage.counter++;
                                MatchCollection vMatchs = Regex.Matches(lineArray[ii], @"((-?\d+)(\.\d+)?)");//读取出lineArray中的小数,并存在数组中
                                double[] vDouble = new double[vMatchs.Count];
                                for (int j = 0; j < vMatchs.Count; j++)//j是为了迭代读取出存储在vMatch中的每一行的小数
                                {
                                    vDouble[j] = double.Parse(vMatchs[j].Value);//store the number of the line into vInts array
                                }
                                stor.NS = vDouble[0];
                                stor.averageStress = vDouble[4];
                                Storage.Dic.Add(Storage.counter, stor);/////出现了大大的问题,字典中的旧值被新值覆盖了
                            }
                        }
                    }
                }
            }
        }
    }
    public class Storage
    {
        public static int counter = 0;//用于对A5E中的字典进行计数(key)
        public double NS
        { get; set; }
        public double averageStress
        { get; set; }
        public static SortedDictionary<int, Storage> Dic = new SortedDictionary<int, Storage>();
    }
}
解决方案

5

Add无法替换舅KeyPair对,假如存在相同key ,将会报错
你可以使用
dic[Storage.counter]=stor;
来进行字典赋值

60

你的 Storage stor = new Storage(); 在循环外
那么循环内 Storage.Dic.Add(Storage.counter, stor); 添加的都是同一个对象
自然是全部 键 的值是相同的(原因是对象传递的是引用:

Quote: 引用:

key相同根本add不进去,会报key已存在

key没有相同,但是旧的value被新的value取代了

key是有可能相同的,原因是Storage.counter = 0;你是放到  foreach (FileInfo finf in inf)里面的,假如有不止一个inf,Storage.counter会归为0,那么就有可能重复啊,是不是应该放在外面?

10

…….
原来你的问题是为什么dic里面全部的sort都是相同的。
原因5L已经回复你了,要修正的话,将那句移动到循环里面

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C#字典(dictionary)中的旧value被新value替换了
喜欢 (0)
[1034331897@qq.com]
分享 (0)