我正在使用一个输入文件,该文件具有公会名称列表,然后是那些公会名称的公会服务器。我的问题是,对于某些行会来说,存在重复,这意味着同一行会存在于不同的服务器上。因此,我需要从输入文件中读取的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);
}
}
我能得到的任何帮助都会很棒。非常感谢
答案 0 :(得分:0)
我想以下可能是您正在寻找的东西,尽管我不确定您到底想做什么(请随时告诉我是否遗漏了东西)。
我通过以下方式实现了以下目标:
xunit
和fluentassertions
nuget程序包。这允许您运行验证该断言的单元测试。
如果我误解了这个问题,请告诉我,我会尽力为您改编。请注意,出于编写测试和验证内容的目的,我从文件输入更改了输入。
using System.Collections.Generic;
using System.IO;
using System.Linq;
using FluentAssertions;
using Xunit;
namespace MultiValueDictionaryTests
{
public class MultiValueDictionaryTests
{
[Fact]
public void UnderstandsPairsCorrectly()
{
/* Mimics the following file contents:
Guild1-ServerDEF
Guild2-ServerDEF
Guild2-ServerABC
Guild1 ServerXYZ
Guild2-ServerABC
Guild2-ServerABC
*/
var testString = "Guild1-ServerDEF\r\nGuild2-ServerDEF\r\nGuild2-ServerABC\r\nGuild1\tServerXYZ\r\nGuild2-ServerABC\r\nGuild2-ServerABC\r\n";
var builder = new GuildBuilder();
var result = builder.BuildGuilds(testString);
result.Should().ContainKey("Guild1");
result.Should().ContainKey("Guild2");
result["Guild1"].Should().ContainKey("ServerDEF")
.And.ContainKey("ServerXYZ");
result["Guild1"].Should().NotContainKey("ServerABC");
result["Guild2"].Should().ContainKey("ServerDEF")
.And.ContainKey("ServerABC");
result["Guild2"].First().Key.Should().Be("ServerABC");
}
[Fact]
public void WriteLine_ShowsCommaSeparatedValues()
{
var builder = new GuildBuilder();
var example = new SortedDictionary<string, SortedList<string, string>>
{
{
"Guild1", new SortedList<string, string> {{"ServerABC", "ServerABC"}, {"ServerDEF", "ServerDEF"}}
},
{
"Guild2", new SortedList<string, string> {{"ServerABC", "ServerABC"}, {"ServerXYZ", "ServerXYZ"}}
}
};
List<string> resultLines = builder.WriteGuildLines(example);
resultLines.First().Should().Be("Guild1 - ServerABC, ServerDEF");
resultLines.Last().Should().Be("Guild2 - ServerABC, ServerXYZ");
}
}
public class GuildBuilder
{
readonly char[] delimiters = { '-', '\t' };
public SortedDictionary<string, SortedList<string,string>> BuildGuilds(string inputString) // instead of file
{
var result = new SortedDictionary<string, SortedList<string,string>>();
using (var reader = new StringReader(inputString))
{
string line;
while ((line = reader.ReadLine()) != null)
{
var splitArray = line.Split(delimiters);
var guild = splitArray[0];
var server = splitArray[1];
if (!result.ContainsKey(guild))
{
result.Add(guild, new SortedList<string,string>());
}
if (!result[guild].ContainsKey(server))
{
result[guild].Add(server, server);
}
}
}
return result;
}
public List<string> WriteGuildLines(SortedDictionary<string, SortedList<string, string>> input)
{
var result = new List<string>();
foreach (var item in input)
{
result.Add($"{item.Key} - {string.Join(", ", item.Value.Keys)}");
}
return result;
}
}
}