2012-05-07 105 views
0

過去三天中,我花時間去以下結構工作,但我將無法弄清楚,如何讓結構工作結合ArrayList的製作結構複雜

array 
    1 => 
    array 
     0 => 
     array 
      'id' => '1' 
      'name' => 'xyz' 
     1 => 
     array 
      'id' => '12' 
      'name' => 'xyz1' 
     2 => 
     array 
      'id' => '54' 
      'name' => 'xyz12' 
    20 => 
    array 
     0 => 
     array 
      'id' => '1' 
      'name' => 'xyz' 
     1 => 
     array 
      'id' => '12' 
      'name' => 'xyz1' 
     2 => 
     array 
      'id' => '54' 
      'name' => 'xyz12' 
     3 => 
     array 
      'id' => '566' 
      'name' => 'xyz1234' 

我嘗試以下的事情,但我將無法繼續前進

Map<Integer, ArrayList<HashMap<String, Object>>> data = new HashMap<Integer, ArrayList<HashMap<String, Object>>>(); 

我有一個結果,因爲我有如下信息

id    | name      | element_id 
______________________________________________________________ 
1    | xyz      | 1 
______________________________________________________________ 
1    | xyz      | 3 
______________________________________________________________ 
12    | xyz1      | 1 
______________________________________________________________ 
54    | xyz11      | 1 
______________________________________________________________ 
566    | xyz1234     | 3 
______________________________________________________________ 
12    | xyz1      | 3 
______________________________________________________________ 
54    | xyz11      | 3 
______________________________________________________________ 

我的代碼是

while (resultSET.next()) 
{ 
    Map<String, Object> tag = new HashMap<String, Object>(); 
    tag.put("name", resultSET.getString(3)); 
    tag.put("id", resultSET.getInt(2)); 

    tags.put(resultSET.getInt(1), tag); 
} 
+4

我沒有看到問題在這裏。你沒有問任何問題。什麼不起作用? – Tudor

+0

你的輸入看起來像一個映射int - > int - > string - > string,所以你可以使用類似於Map >>,但使用起來會很麻煩。 – tonio

+0

我更新了問題,我的代碼以及..!如何讓代碼工作多數民衆贊成在問題 – KuKu

回答

1

你應該嘗試

Map<Integer, Map<Integer, Map<Integer, Integer>>> result = new HashMap<Integer, Map<Integer,Map<Integer,Integer>>>(); 

凡在最後的地圖,你將存儲的值

'id' => '1' 
      'name' => 'xyz' 

和第二級地圖,你應該存儲

0 => 
     array 
      'id' => '1' 
      'name' => 'xyz' 

,決賽外地圖,你應該存儲

1 => 
    array 
     0 => 
     array 
      'id' => '1' 
      'name' => 'xyz' 
     1 => 
     array 
      'id' => '12' 
      'name' => 'xyz1' 
     2 => 
     array 
      'id' => '54' 
      'name' => 'xyz12' 

希望這對你的作品

享受!

+0

我做到了,我想要的方式..! [鏈接](HTTP://計算器。com/questions/10487450/hashmap-with-arraylist-making-making-complex-structure/10493303#10493303) – KuKu

0
Map<String, Object> tag = new HashMap<String, Object>(); 
tag.put("name", resultSET.getString(3)); 
tag.put("id", resultSET.getInt(2)); 

你可以做的一個明顯的簡化就是使標記具有屬性name和id的類。你有什麼理由不想這樣做?


關於與當前的代碼的問題:

tags.put(resultSET.getInt(1), tag); 

您需要創建一個包含標籤的數組列表,然後放進了標籤。

+0

是的,這是做其它方式,但作爲它的j2ee應用程序,那麼我已經使該PUBLIC有PUBLIC屬性的類,我不想要 – KuKu

2

當處理集合集合(集合...)時,事情往往會很快變得混亂。嘗試使用(描述性命名)對象封裝不同的集合,並且我懷疑它會更容易管理。

0

這並不困難......!終於得到了... !!

HashMap<Integer, ArrayList<HashMap<String, Object>>> tags = new HashMap<Integer, ArrayList<HashMap<String, Object>>>(); 

while (routeRs1.next()) 
{ 
    ArrayList<HashMap<String, Object>> temp_tags = new ArrayList<HashMap<String, Object>>(); 
    HashMap<String, Object> tag  = new HashMap<String, Object>(); 

    if(tags.get(routeRs1.getInt(1)) == null) 
    { 
     tag.put("name", routeRs1.getString(3)); 
     tag.put("id", routeRs1.getInt(2)); 

     temp_tags.add(tag); 

    } 
    else 
    { 
     temp_tags = (ArrayList<HashMap<String, Object>>) tags.get(routeRs1.getInt(1)); 

     tag.put("name", resultSET.getString(3)); 
     tag.put("id", resultSET.getInt(2)); 
     temp_tags.add(tag); 
    } 
    tags.put(resultSET.getInt(1), temp_tags); 
} 

簡單的軟件概念就是打破問題......! :D