1

首先,我对 C# 非常陌生,我正在尝试重新创建我在 Java 中创建的应用程序。

我有 4 个列表框。每个框都将保存 xml 文件中的值列表。

listBox_year 为<Year>. listBox_make 为<Make>. listBox_model 为<Model>. listBox_subModel 用于<sub-Model>.

所以假设我将所有年份添加到 listBox_year 中,没有重复年份。假设我单击某一年,它将调出该年的所有汽车品牌。然后我点击Make,它会显示那个年份的那个Make的模型等等......

使用 Java,我能够使用 HashMap 来完成这项工作,我可以在其中拥有多个同名键,并且我可以搜索在这种情况下选择了任何键的年份获取所有具有该年份的 Makes 或 Values 作为钥匙。

这是XML格式

<?xml version="1.0" encoding="utf-8" ?>
<vehicles>

  <Manufacturer>
    <Make>Subaru</Make>
    <Year>2010</Year>
    <Model>Impreza</Model>
    <Sub-Model>2.0i</Sub-Model>
    <Highway>36 MPG highway</Highway>
    <City>27 MPG city</City>
    <Price>$17,495</Price>
    <Description>
      Symmetrical All-Wheel Drive. 
      SUBARU BOXER® engine. 
      Seven airbags standard. 
      >Vehicle Dynamics Control (VDC). 
    </Description>
  </Manufacturer>

  <Manufacturer>
    <Make>Toyota</Make>
    <Year>2012</Year>
    <Model>Supra</Model>
    <Sub-Model>TT</Sub-Model>
    <Highway>22 MPG highway</Highway>
    <City>19 MPG city</City>
    <Price>$48,795</Price>
    <Description>
      16-inch aluminum-alloy wheels.
      6-speaker audio system w/iPod® control.
      Bluetooth® hands-free phone and audio.
      Available power moonroof.
    </Description>
  </Manufacturer>

  <Manufacturer>
    <Make>Subaru</Make>
    <Year>2011</Year>
    <Model>Impreza</Model>
    <Sub-Model>2.0i Limited</Sub-Model>
    <Highway>36 MPG highway</Highway>
    <City>27 MPG city</City>
    <Price>$18,795</Price>
    <Description>
      16-inch aluminum-alloy wheels. 
      6-speaker audio system w/iPod® control. 
      Bluetooth® hands-free phone and audio. 
      Available power moonroof.
    </Description>
  </Manufacturer>

  <Manufacturer>
    <Make>Subaru</Make>
    <Year>2011</Year>
    <Model>Impreza</Model>
    <Sub-Model>2.0i Limited</Sub-Model>
    <Highway>36 MPG highway</Highway>
    <City>27 MPG city</City>
    <Price>$18,795</Price>
    <Description>
      16-inch aluminum-alloy wheels.
      6-speaker audio system w/iPod® control.
      Bluetooth® hands-free phone and audio.
      Available power moonroof.
    </Description>
  </Manufacturer>

</vehicles>
4

1 回答 1

1

最接近 java hashmap 的类型是 Dictionary。由于您需要具有相同密钥的多个项目,因此我会使用Dictionary<int,List<Item>>. 以下是您可能需要的一些基本功能:

void AddItem(int key, Item i, Dictionary<int,List<Item>> dict)
{
   if (!dict.ContainsKey(key))
   {
      dict.Add(i,new List<Item>());
   }
   dict[key].Add(i);
}

List<Item> GetList(int key)
{
   if (dict.ContainsKey(key))
   {
      return dict[key];
   }
   else
   {
      return new List<Item>(); // can also be null
   }
}
于 2012-05-13T22:46:34.757 回答