【发布时间】:2019-02-09 22:18:00
【问题描述】:
我正在处理一个包含公会名称列表的输入文件,然后是这些公会名称的公会服务器。我的问题是,对于某些公会存在重复,这意味着相同的公会存在于不同的服务器上。所以我需要从我的输入文件中读取的 guildName 来显示它所在的不同服务器。这是我到目前为止的代码:
private void buildGuilds()
{
using (StreamReader inFile = new StreamReader("guilds.txt"))
{
string str = null;
char[] delimiters = {'-', '\t'};
//while the buffer isn't empty
while ((str = inFile.ReadLine()) != null)
{
SortedList<string, string> guild = new SortedList<string, string>();
string[] Buffer = str.Split(delimiters);
guildName = Buffer[1];
guildServer = Buffer[2];
if (!dictGuilds.ContainsKey(guildName))
{
dictGuilds.Add(guildName, new List<string>());
}
dictGuilds[guildName].Add(guildServer);
所以我的程序将数据读入 2 个变量,然后确定哪些值在哪里,但是在使用传统的 foreach pair.Key pair.Value 方法时我无法打印它。这也是我的打印方法。
private void printGuilds()
{
foreach (KeyValuePair<string, List<string>> pair in dictGuilds)
{
Guilds_List.Items.Add(pair.Key + pair.Value);
}
}
我能得到的任何帮助都会很棒。非常感谢你
【问题讨论】:
-
看起来你已经正确地建立了服务器列表的名称......所以打印列表stackoverflow.com/questions/52927/…(当前为重复)应该可以工作。如果您还有其他问题 - edit 发布并澄清。还要确保帖子中的代码是minimal reproducible example,而不是一些代码中间的一些行集合。
标签: c#