【问题标题】:Java HashMap to a C# DictionaryJava HashMap 到 C# 字典
【发布时间】:2020-01-29 13:19:00
【问题描述】:

我在 Java 中使用HashMap,如下所示:

Map<Player, List<Attribute>> map = new HashMap<>();

map.put(
  player("Lebron James"), 
  attribute("mid height", "mid weidght", "high vertical")
);

效果很好,但现在我想它转换成C#,使用Dictionary:

Dictionary<Player, List<Attribute>> dictionary = new Dictionary<Player, List<Attribute>>();

dictionary.Add(
  player("Kobe Bryant"), // <- Doesn't compile
  attribute("mid height", "mid weidght", "high vertical")
);

谁能解释一下,为什么C#代码不能编译?消息是

无法调用的播放器组件不能作为方法使用

【问题讨论】:

  • 您能否明确告诉我们什么是不起作用以及结果如何?是否有错误(消息)?
  • dictionary.Add of a Dictionary&lt;Player, List&lt;Attribute&gt;&gt; 期望 List&lt;Attribute&gt; 作为第二个参数,但您只传递一个 Attribute 而不是一个列表。这正是编译器错误消息(您忘记包含在您的问题中)告诉您的内容。
  • "无法调用的播放器组件不能作为方法使用"
  • new Player("Kobe Bryant") 而不是 player("Kobe Bryant")?
  • 如果没有 new !..,您的 Java 代码也不应该工作。如果该 Java 代码确实工作,这意味着您有称为 playerattribute 的方法返回一个 @分别为 987654334@ 实例和 List&lt;Attribute&gt;,这也可以在 C# .NET 代码中完成..

标签: java c# dictionary hashmap


【解决方案1】:

仅供参考:使用new 的接受答案不是提供的 Java 答案的端口。最终结果可能是相同的(我实际上建议使用新实例而不是称为 playerattribute 的方法),但这不是 Java 答案的端口。我主要想在这个答案中澄清一下,以防其他人将来遇到这个 SO 问题和答案。

根据 Java 代码,您的程序似乎应该包含 方法 player(String)attribute(String, String, String),它们返回 Player-instance 和 @987654331 @-instance 分别。因此,您应该在 C# .NET 程序中创建相同的方法。

您当前的 Java sn-p 不能按原样工作,实际上会导致类似的错误! Try it online.

使用new 直接创建PlayerList&lt;Attribute&gt; 的新实例的公认答案可能会在两个程序中工作并给出相同的结果,但它不是直接端口 em> 您当前的 Java 实现!否则你的 Java 代码会变成这样:

Map<Player, List<Attribute>> map = new HashMap<>();
map.put(
  new Player("Lebron James"), 
  new ArrayList<Attribute>(){{
    add(new Attribute("mid height"));
    add(new Attribute("mid weidght"));
    add(new Attribute("high vertical"));
  }}
);

Try it online.

假设您更完整的 Java 代码是这样的:

  ...

  Map<Player, List<Attribute>> map = new HashMap<>();
  map.put(
    player("Lebron James"), 
    attribute("mid height", "mid weidght", "high vertical")
  );
}

private Player player(String name){
  return new Player(name);
}

private List<Attribute> attribute(String... strAttributes){
  List<Attribute> resultList = new ArrayList<>();
  for(String strAttr : strAttributes){
    resultList.add(new Attribute(strAttr));
  }
  return resultList;
}

Try it online.

移植的 C# .NET 代码将变为:

  ...

  IDictionary<Player, IList<Attribute>> dictionary = new Dictionary<Player, IList<Attribute>>();
  dictionary.Add(
    player("Lebron James"), 
    attribute("mid height", "mid weidght", "high vertical")
  );
}

private Player player(string name){
  return new Player(name);
}

private IList<Attribute> attribute(params string[] strAttributes){
  IList<Attribute> resultList = new List<Attribute>();
  foreach(string strAttr in strAttributes){
    resultList.Add(new Attribute(strAttr));
  }
  return resultList;
}

Try it online.

再次,我主要为将来遇到此问题和答案的其他人创建此答案,并澄清接受的答案不是提供的 Java sn-p 的直接端口。

【讨论】:

  • 不要太挑剔,但更准确的转换是将'map'声明为接口,如原始代码:IDictionary> dictionary = new Dictionary>();
  • @DaveDoknjas 你是完全正确的,固定的。这同样适用于List/ArrayListIList/List。感谢您的关注。
【解决方案2】:

您可能想要创建实例 - PlayerList&lt;Attribute&gt; 及其项目。

dictionary.Add(
  new Player("Kobe Bryant"), 
  new List<Attribute>() {
    new Attribute("mid height"), 
    new Attribute("mid weidght"), 
    new Attribute("high vertical"),
  }
);

【讨论】:

  • 谢谢!我认为从 Java 转换为 C# 会更容易。
  • 这不是一个准确的转换 - 你在胡乱猜测。更重要的是,似乎缺少 'player' 和 'attribute' 方法 - 处理这是核心问题。
猜你喜欢
  • 2011-10-16
  • 1970-01-01
  • 1970-01-01
  • 2011-03-10
  • 1970-01-01
  • 1970-01-01
  • 2018-01-16
  • 2017-07-01
  • 1970-01-01
相关资源
最近更新 更多