【问题标题】:Looping through an array to compare object properties without duplicating循环遍历数组以比较对象属性而不重复
【发布时间】:2020-09-23 21:07:17
【问题描述】:

我正在尝试围绕网格移动对象并计算战斗(当它们接触时),但每次我得到一个交互时,它们都会计算两次。我明白为什么,但想不出办法。

代码:

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

        // Check to see if any of the objectsare touching
        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("FIGHT");
                            System.out.println("There is a fight between " + objectArray[i].getName() + " and " + objectArray[j].getName() + "." );

                            fights++;

                        }
                    }
                }
            }
        }

【问题讨论】:

    标签: java oop object


    【解决方案1】:

    您正在浏览整个列表,对于列表中的每个元素,您...浏览整个列表。再次。

    不要那样做 - 如果您希望列表中的每个项目与列表中的其他项目“相遇”,那么,想想第一个循环:第一个“战斗机”将在您的循环中相遇自己(j = 0),然后遇到所有其他战士。您使用 if(第一个 if)消除了“遇见自己”选项。

    然后下一个战士 (i = 1) 将从头开始:它会遇到第一个战士 (j = 0),重复我们已经看到的战斗,然后遇到自己,然后遇到场上的其他人。

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

    【讨论】:

    • 比我的回答好多了。
    • @rzwitserloot,如果当 i 为 2 时,这是否意味着从 3 开始并且只比较元素 3 和 4?因为元素 0 和 1 可能位于同一位置,我需要考虑这一点。
    • *j 从 3 开始。
    • 您不需要这样做,因为您已经在之前的循环中比较了这两个元素。
    【解决方案2】:

    您可以创建循环属性,这可能不是最佳答案,但它可以确保您不会比较数组中相同的两个对象。

    代码: // 循环属性 字符串 lastFightObejectOne = "", lastFightObjectTwo = "";

            // Move all the objects
            for(int i = 0; i < objectArray.length; i++) {
                objectArray[i].move();
            }
    
            // Check to see if any of the objects are touching
            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(!lastFightObejectOne.equal(objectArray[i].getName()) && (!lastFightObejectTwo.equal(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();
                                lastFightObectTwo = objectArray[j].getName();
    
                                fights++;
                                }
                            }
                        }
                    }
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多