1

我正在尝试遍历两个 ArrayList(称为 sceneObjects 和 animationSceneObjects)并根据名称字段匹配两者。这是我的代码:

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 迭代器的值进行比较。

谁能发现我做错了什么?

谢谢你。

4

4 回答 4

1

您必须itrMain在每次运行内部循环之前重置,否则 - 一旦您第一次用尽迭代器 - 将永远不会调用内部循环。

您可以通过在到达内部循环之前重新分配它来做到这一点sceneObjects.iterator(),或者使用增强的 for-each 循环

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;
                }
            }
        }
于 2012-05-10T12:25:52.633 回答
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...
    }
}
于 2012-05-10T12:26:05.520 回答
1

您不会为每个外部循环步骤重新启动内部迭代器。你真正应该做的是使用 for-each 循环。

for (AnimationObject ao : animationObjects) {
  ... your outer-loop code ...
  for (SceneObject so : sceneObjects) {
     ... your inner-loop code ...
  }
]
于 2012-05-10T12:26:25.910 回答
1

您必须在第一个循环中声明 itrMain 迭代器。如果你保持相同的迭代器,你的第一次迭代将消耗它。

Iterator<AnimationObject> itr = animationObjects.iterator();
while (itr.hasNext()) {
    Iterator<SceneObject> itrMain = sceneObjects.iterator();
    [...]
于 2012-05-10T12:26:37.763 回答