1

I am trying to iterate through two ArrayLists (called sceneObjects and animationSceneObjects) and match the two based on a name field. Here is my code :

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;
                }
            }
        }

The problem is that the code isn't working as intended - it only compares the first item in the itr iterator to the values of the itrMain iterator.

Can anyone spot what I have got wrong ?

Thank you.

4 Answers 4

1

You will have to reset itrMain before each run of the inner loop, otherwise - once you have exhausted the iterator the first time - the inner loop will never be invoked.

You can do it by re-assigning it with sceneObjects.iterator() before you reach the inner loop, or use an enhanced 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;
                }
            }
        }
Sign up to request clarification or add additional context in comments.

Comments

1

Use a new iterator for the inner loop ever time:

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;
        }
    }
}

or (perhaps more simply) use two for loops:

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

Comments

1

You're not restarting the inner iterator for every outer loop step. What you should really do is use the for-each loop.

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

Comments

1

You have to declare the itrMain iterator inside the first loop. If you keep the same iterator, your first iteration will have consumed it.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.