循环遍历数组以比较对象属性,无需复制。

huangapple go评论69阅读模式
英文:

Looping through an array to compare object properties without duplicating

问题

// 移动所有的物体
for (int i = 0; i < objectArray.length; i++) {
    objectArray[i].move();
}

// 检查是否有任何物体相互接触
for (int i = 0; i < objectArray.length; i++) {

    for (int j = 0; j < objectArray.length; j++) {

        if (objectArray[i] != objectArray[j]) {

            if (objectArray[i].getX() == objectArray[j].getX() && objectArray[i].getY() == objectArray[j].getY()) {
                System.out.println("冲突");
                System.out.println(objectArray[i].getName() + " 和 " + objectArray[j].getName() + " 之间发生了冲突。");
                fights++;
            }
        }
    }
}
英文:

I am trying to move objects around a grid and counting the fights(when they touch) but every time I get one interaction they it counts it twice. I understand why, but cannot think of a way around it.

code:

        // Move all the objects
        for(int i = 0; i &lt; objectArray.length; i++) {
            objectArray[i].move();
        }

        // Check to see if any of the objectsare touching
        for(int i = 0; i &lt; objectArray.length; i++) {

            for(int j = 0; j &lt; objectArray.length; j++) {

                if(objectArray[i] != objectArray[j]) {

                    if(objectArray[i].getX() == objectArray[j].getX() &amp;&amp; objectArray[i].getY() == objectArray[j].getY()) {


                            System.out.println(&quot;FIGHT&quot;);
                            System.out.println(&quot;There is a fight between &quot; + objectArray[i].getName() + &quot; and &quot; + objectArray[j].getName() + &quot;.&quot; );

                            fights++;

                        }
                    }
                }
            }
        }

答案1

得分: 1

你正在遍历整个列表,对于列表中的每个元素,你会再次遍历整个列表。

不要这样做 - 如果你希望列表中的每个项目都与列表中的其他项目“相遇”,那么首先考虑一下那个第一次循环:在你的循环中,第一个“战士”会与自己相遇(j = 0),然后与所有其他战士相遇。你可以通过一个 if 语句(第一个 if)来消除“与自己相遇”的选项。

然后下一个战士(i = 1)会再次从头开始:它会与第一个战士(j = 0)相遇,重复我们已经看到的战斗,然后与自己相遇,然后再与其余战士相遇。

解决方案是不要从“0”开始内部循环,而是从自己之后的下一个战士开始:第二个战士首先应该与第三个战士相遇(跳过第一个和自己)。所以,int j = i + 1,而不是 int j = 0,这就是你所需要的。现在你也可以完全消除“不要与自己战斗”的那一行,因为现在这种情况不可能发生。

英文:

you're going through the entire list, and for each element in the list you... go through the entire list. again.

Don't do that - if you want each item in the list to 'meet' each other item in the list, then, well, think of that first loop: The first 'fighter' will, in your loop, meet itself (j = 0), and then meet all other fighters. You eliminate the 'meet yourself' option with an if (the first if).

Then the next fighter (i = 1) will start from the beginning again: It will meet the first fighter (j = 0), repeating the fight we already saw, then meet itself, then meet the rest of the field.

The solution is to start the inner loop not at '0', but at the next fighter after yourself: That second fighter should first meet the third (and skip the first, and itself). So, int j = i + 1, not int j = 0, that's all you need. You can then also eliminate the 'do not fight yourself' if line entirely, as that cannot happen now.

答案2

得分: 0

// 循环属性
String lastFightObjectOne = "", lastFightObjectTwo = "";

// 移动所有对象
for (int i = 0; i < objectArray.length; i++) {
objectArray[i].move();
}

// 检查是否有任何对象相触
for (int i = 0; i < objectArray.length; i++) {

for (int j = i + 1; j < objectArray.length; j++) {

    if (objectArray[i] != objectArray[j]) {

        if (objectArray[i].getX() == objectArray[j].getX() && objectArray[i].getY() == objectArray[j].getY()) {

            if (!lastFightObjectOne.equals(objectArray[i].getName()) && !lastFightObjectTwo.equals(objectArray[j].getName())) {
                System.out.println("FIGHT");
                System.out.println("There is a fight between " + objectArray[i].getName() + " and " + objectArray[j].getName() + ".");

                lastFightObjectOne = objectArray[i].getName();
                lastFightObjectTwo = objectArray[j].getName();

                fights++;
            }
        }
    }
}

}

英文:

You could make loop properties, probably not the best answer but it will work to make sure you don't compare the same two objects in the array.

code:
// Loop properties
String lastFightObejectOne = "", lastFightObjectTwo = "";

        // Move all the objects
        for(int i = 0; i &lt; objectArray.length; i++) {
            objectArray[i].move();
        }

        // Check to see if any of the objects are touching
        for(int i = 0; i &lt; objectArray.length; i++) {

            for(int j = i + 1; j &lt; objectArray.length; j++) {

                if(objectArray[i] != objectArray[j]) {

                    if(objectArray[i].getX() == objectArray[j].getX() &amp;&amp; objectArray[i].getY() == objectArray[j].getY()) {

                            if(!lastFightObejectOne.equal(objectArray[i].getName()) &amp;&amp; (!lastFightObejectTwo.equal(objectArray[j].getName())) {
                            System.out.println(&quot;FIGHT&quot;);
                            System.out.println(&quot;There is a fight between &quot; + objectArray[i].getName() + &quot; and &quot; + objectArray[j].getName() + &quot;.&quot; );

                            lastFightObjectOne = objectArray[i].getName();
                            lastFightObectTwo = objectArray[j].getName();

                            fights++;
                            }
                        }
                    }
                }
            }
        }

huangapple
  • 本文由 发表于 2020年9月24日 05:07:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64036224.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定