2014-02-19 12 views
0

我有一個名爲ionList的列表,其中包含名爲structs的值。 列表中的每個結構都有更多的列表,這些列表中有更多的結構。你可以想象它像一種父母名單和子列表如何從列表中獲取具有子列表的值並將所有值按順序存儲在另一個列表中

我想檢索ionList中的所有結構,並將其存儲在一個名爲StructList的列表中。

我使用的代碼是遞歸代碼,但是當我調用遞歸函數whenevr時,我遇到了我的ionList中的子列表時數據正在丟失。

public List<IonStruct> findStructsFromParentList(IonList ionList) { 
     String objectType = "object_type"; 
     String diffType = "diff_type"; 
     String childList = "children"; 
     IonList ionChildList = null; 
     IonList ionChildList1 = null; 
     List<IonStruct> ionStructList = new ArrayList<IonStruct>(); 
     System.out.println("ION LIST"+ionList.size()); 
     for (int index = 0; index < ionList.size();index++) { 
      ionStructList.add((IonStruct) ionList.get(index)); 
      ionChildList = (IonList) ionStructList.get(index).get(childList); 
      if (ionChildList != null) { 
       System.out.println("Found child"); 
       ionStructList.addAll(findStructsFromParentList(ionChildList)); 

      } 

     } 

     return ionStructList; 


    } 

的的childList發現正在改變我對環路初始化在我的遞歸調用我認爲這是不解析一個以上的結構與它的孩子名單的原因。請幫助我..

謝謝!!!

+0

我只是盯着這個問題,當你剛纔問了一個較長的版本。突然你刪除了它,我無法回答! :-( –

+0

使用調試器找出for循環對所有列表項是否執行,我覺得可能會在方法findStructsFromParentList(ionChildList)中拋出一些異常 – AJJ

+0

我也沒有刪除該帖子...我覺得它很混亂刪除它 –

回答

3

,說

ionChildList = (IonList) ionStructList.get(index).get(childList); 

應該說

ionChildList = (IonList)((IonStruct) ionList.get(index)).get(childList); 

因爲內ionStructList,該指標會有所不同,所以用你的代碼,你不會取得右側列表行。

+0

嘿,工作:)謝謝很多人:) –

+0

如果你覺得這是正確的答案,更好接受! –

相關問題