2012-05-13 30 views
1

首先,我非常新的C#即時通訊嘗試重新創建一個我用Java創建的應用程序。鏈接4列表框

我有4個Listboxes。每個框將保存來自xml文件的值列表。

listBox_year爲<Year>。 listBox_make爲<Make>。 listBox_model爲<Model>。 listBox_subModel用於<sub-Model>

因此,讓我說我把所有的年份添加到listBox_year沒有重複的年份。假設我點擊一年,它會提出當年所有的汽車製造商。然後我點擊Make,它會調出那個在那年製作的模型等等......

用Java我能夠使用HashMap來使這個工作到我可以有多個鍵的地方相同的名稱,我可以搜索在這種情況下,選擇年份的關鍵字抓取所有以該年爲關鍵字的「所有」或「值」。

下面是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> 
+1

您的問題是什麼?你在找'Dictionary'嗎? – SLaks

+0

即時通訊不知道該集合被稱爲,但似乎@lcfseth很快回答了這個問題。 –

回答

1

最接近類型到Java散列映射是字典。由於您需要擁有多個具有相同密鑰的項目,因此我會使用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 
    } 
} 
+0

非常感謝你,我會試試這個。有一段時間我沒有去過這個網站。 –