2

(在此 Windows 窗体应用程序中)我正在尝试将文件中的数据读取到哈希表中,并使用哈希表中的数据填充文本框,但是当我运行代码时,我总是抛出异常“项目已经已添加。在字典中键入:''正在添加的键:''“

初始代码:

string[] fileLines = File.ReadAllLines(@"C:path/ajand.txt");

foreach (string line in fileLines)
{
    // to split the first 9 chars in the string and use them as key values                
    string[] match = Regex.Split(line, line.Substring(0,9));
    hT.Add(match[0], line);
}

所以我尝试使用以下代码检查关键重复项

 string[] fileLines = File.ReadAllLines(@"C:path/ajand.txt");

 foreach (string line in fileLines)
 {
     string[] match = Regex.Split(line, line.Substring(0,9));

     if(!hT.ContainsKey(match[0])) // to check duplicates
     hT.Add(match[0], line);            
 }

但是当我运行程序时,相应的文本框没有填充“似乎”已添加到哈希表中的数据。请任何想法是什么问题。

4

4 回答 4

1

如果我理解正确,您可以使用这样的功能:

public static Dictionary<string, string> LoadActivityLookup(string filePath) {
    const int KEY_LENGTH = 10;
    var newLookup = new Dictionary<string, string>();
    foreach (string line in File.ReadAllLines(filePath)) {
        if (line.Length < KEY_LENGTH) continue;
        string key = line.Substring(0, KEY_LENGTH);
        if (!newLookup.ContainsKey(key)) {
            string value = line.Substring(KEY_LENGTH);
            newLookup.Add(key, value);
        }
    }
    return newLookup;
}

字典非常适​​合在大量键中重复查找键。但是,如果您只需要在集合中保存一堆键/值对以便以后可以遍历它们,那么我会使用 List<> 代替。

这是上述函数的一个版本,它使用 StreamReader 而不是将完整的文件加载到字符串数组中。

public static Dictionary<string, string> LoadActivityLookup(string filePath) {
    const int KEY_LENGTH = 10;
    var newLookup = new Dictionary<string, string>();
    using (StreamReader rdr = new StreamReader(filePath)) {
        string line = rdr.ReadLine();
        while (line != null) {
            if (line.Length < KEY_LENGTH) {
                line = rdr.ReadLine();
                continue;
            } 
            string key = line.Substring(0, KEY_LENGTH);
            if (!newLookup.ContainsKey(key)) {
                string value = line.Substring(KEY_LENGTH);
                newLookup.Add(key, value);
            }
            line = rdr.ReadLine();
        }
    }
    return newLookup;
}
于 2012-11-28T21:59:48.403 回答
1
    public static Hashtable HashtableFromFile(string path)
    {
        try
        {
            using (FileStream stream = new FileStream(path, FileMode.Open))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                return (Hashtable)formatter.Deserialize(stream);
            }
        }
        catch
        {
            return new Hashtable(); 
        }            
    }
于 2015-12-27T19:43:09.510 回答
0

为了删除重复项,您可以简单地使用索引来添加/更新哈希表:

string[] fileLines = File.ReadAllLines(@"C:path/ajand.txt");

foreach (string line in fileLines)
{
    // to split the first 10 chars in the string and use them as key values                
    string key = line.Length > 10 ? line.Substring(0,10) : line;
    hT[key] = line;
}
于 2012-11-28T20:29:17.517 回答
0

看看这是否有帮助。它假定日期和行的其余部分之间的分隔符是一个空格。它将尝试将日期解析为 DateTime 对象并将其用作键。它将使用该行的其余部分作为值。索引器用于将值放入字典这一事实消除了重复键的问题,但同时如果您有具有相同日期的条目,它将覆盖值。在这种情况下,只有具有该日期的最后一个条目将保存到字典中。

var lines = File.ReadAllLines(@"C:\path\ajand.txt");

foreach (var line in lines) {
    var index = line.Trim().IndexOf(" ");
    if (index == -1) {
        continue;
    }

    DateTime key;
    if (DateTime.TryParseExact(line.Substring(0, index), out key)) {
        hT[key] = line.Substring(index);
    }
}
于 2012-11-29T00:20:26.740 回答