English 中文(简体)
借助两名ArrayList与“ Nest号”声明
原标题:Iterating through two ArrayList with Nested While Statements - Java

我正试图通过2名阿雷拉主义者(所谓的现场物体和动画物体)播音,并根据一个名称领域对这两个人进行对比。 我的法典如下:

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.

谁能发现我会错过什么?

谢谢。

最佳回答
问题回答

a. 任何时候使用新的炉 the:

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

你们不要重新为每一个外部飞行步骤重新启动内幕。 你们应该真正做的是使用 for。

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

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();
    [...]




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签