2012-05-10 53 views
1

我想通過兩個ArrayLists(稱爲sceneObjects和animationSceneObjects)進行迭代並匹配基於名稱字段的兩個ArrayList。這裏是我的代碼:使用嵌套while語句迭代兩個ArrayList - Java

Iterator<AnimationObject> itr = animationObjects.iterator(); 
Iterator<SceneObject> itrMain = sceneObjects.iterator(); 

while (itr.hasNext()) { 
      AnimationObject object = itr.next(); 
      //Remove the word node from the animation object name so it matches main object name. 
      String tmpString = object.animationobjectName.replace("node-", ""); 
      System.out.println("Animation Object name is" +tmpString); 
      while (itrMain.hasNext()) { 
       SceneObject objectMain = itrMain.next(); 
       System.out.println("Scene Object name is" +objectMain.objectName); 
       if (tmpString.equals(objectMain.objectName)) { 
        System.out.println("Animation Object matched to main object array" +tmpString); 
        objectMain.animations = object.animationData; 
        objectMain.hasAnimations = true; 
       } 
      } 
     } 

的問題是,該代碼並不如預期運行 - 這只是第一個項目在ITR迭代器itrMain迭代器的值進行比較。

任何人都可以發現我有什麼問題嗎?

謝謝。

回答

1

在內循環的每次運行之前,您必須重置itrMain,否則 - 一旦您第一次耗盡迭代器 - 內循環將永遠不會被調用。

您可以通過重新分配它sceneObjects.iterator()做你到達內循環之前,或使用增強for-each loop

Iterator<AnimationObject> itr = animationObjects.iterator(); 

while (itr.hasNext()) { 
      Iterator<SceneObject> itrMain = sceneObjects.iterator(); 
     // ^
     // reassigning itrMain each iteration of the outer loop 

      AnimationObject object = itr.next(); 
      //Remove the word node from the animation object name so it matches main object name. 
      String tmpString = object.animationobjectName.replace("node-", ""); 
      System.out.println("Animation Object name is" +tmpString); 
      while (itrMain.hasNext()) { 
       SceneObject objectMain = itrMain.next(); 
       System.out.println("Scene Object name is" +objectMain.objectName); 
       if (tmpString.equals(objectMain.objectName)) { 
        System.out.println("Animation Object matched to main object array" +tmpString); 
        objectMain.animations = object.animationData; 
        objectMain.hasAnimations = true; 
       } 
      } 
     } 
+0

太棒了 - 謝謝大家。 – GuybrushThreepwood

1

使用新的迭代器內環有史以來時間:

Iterator<AnimationObject> itr = animationObjects.iterator(); 

while (itr.hasNext()) { 
    AnimationObject object = itr.next(); 
    //Remove the word node from the animation object name so it matches main object name. 
    String tmpString = object.animationobjectName.replace("node-", ""); 
    System.out.println("Animation Object name is" +tmpString); 

    Iterator<SceneObject> itrMain = sceneObjects.iterator(); 

    while (itrMain.hasNext()) { 
     SceneObject objectMain = itrMain.next(); 
     System.out.println("Scene Object name is" +objectMain.objectName); 
     if (tmpString.equals(objectMain.objectName)) { 
      System.out.println("Animation Object matched to main object array" +tmpString); 
      objectMain.animations = object.animationData; 
      objectMain.hasAnimations = true; 
     } 
    } 
} 

或(也許更簡單)使用兩個for循環:

for (AnimationObject animation : animationObjects) { 
    for (SceneObject scenes : sceneObjects) { 
     // snip... 
    } 
} 
1

你沒有爲每個外部循環步驟重新啓動內部迭代器。你應該真正做的是使用for-each循環。

for (AnimationObject ao : animationObjects) { 
    ... your outer-loop code ... 
    for (SceneObject so : sceneObjects) { 
    ... your inner-loop code ... 
    } 
] 
1

您必須在第一個循環內聲明itrMain迭代器。 如果你保持相同的迭代器,你的第一次迭代將會消耗它。

Iterator<AnimationObject> itr = animationObjects.iterator(); 
while (itr.hasNext()) { 
    Iterator<SceneObject> itrMain = sceneObjects.iterator(); 
    [...]