【发布时间】:2014-03-29 18:07:18
【问题描述】:
我想做一本包含另一本字典的字典。我希望我的字典只有一个键有多个值,但我得到一个错误:“具有相同键的元素已经存在”。我已经用 HashMap 在 JAVA 中做到了,没关系,但在 C# 中......
类似这样的:
static Dictionary<int, Dictionary<double, double>> _dict = new Dictionary<int, Dictionary<double, double>>()
{
{ 1, new Dictionary<double,double>{ { 0.990, 0.0316 } } },
{ 1, new Dictionary<double,double>{ { 0.975, 0.0398 } } },
{ 1, new Dictionary<double,double>{ { 0.950, 0.0239 } } },
{ 1, new Dictionary<double,double>{ { 0.9 , 0.0158 } } },
{ 1, new Dictionary<double,double>{ { 0.1 , 2.71 } } },
{ 1, new Dictionary<double,double>{ { 0.050, 3.84 } } },
{ 1, new Dictionary<double,double>{ { 0.025, 5.02 } } },
{ 1, new Dictionary<double,double>{ { 0.010, 6.63 } } },
{ 2, new Dictionary<double,double>{ { 0.990, 0.02 } } },
{ 2, new Dictionary<double,double>{ { 0.975, 0.05 } } },
{ 2, new Dictionary<double,double>{ { 0.950, 0.10 } } },
{ 2, new Dictionary<double,double>{ { 0.9 , 0.21 } } },
{ 2, new Dictionary<double,double>{ { 0.1 , 4.60 } } },
{ 2, new Dictionary<double,double>{ { 0.050, 5.99 } } },
{ 2, new Dictionary<double,double>{ { 0.025, 7.38 } } },
{ 2, new Dictionary<double,double>{ { 0.010, 9.21 } } },
{ 3, new Dictionary<double,double>{ { 0.990, 0.12 } } },
{ 3, new Dictionary<double,double>{ { 0.975, 0.22 } } },
{ 3, new Dictionary<double,double>{ { 0.950, 0.35 } } },
{ 3, new Dictionary<double,double>{ { 0.9 , 0.58 } } },
{ 3, new Dictionary<double,double>{ { 0.1 , 6.25 } } },
{ 3, new Dictionary<double,double>{ { 0.050, 7.81 } } },
{ 3, new Dictionary<double,double>{ { 0.025, 9.35 } } },
{ 3, new Dictionary<double,double>{ { 0.010, 11.34 } } },
};
我尝试了 LordTakkera 的解决方案。
我这样做是因为我必须代表 KHI2 表。我在 JAVA 中执行此操作,我的软件完美运行,但我不知道如何在 .NET 中使用 REF。
【问题讨论】:
-
为什么要一个键有多个值?你怎么知道你从那个键获得了什么价值?
-
你不能用
Dictionary来做到这一点。在C#字典不能保存重复的键。 -
Java 中的相同代码没有意义,因为您正在覆盖您放入 Map 中的数据。
标签: java c# dictionary idictionary