将同一组内的项目相互比较

时间:2013-05-20 04:58:43

标签: java list loops foreach set

我有一组形状,需要确定哪些形状相交。我目前使用两个增强的 - for - 循环(一个在另一个循环中)但它没有按预期工作。似乎只是将Shape与自身进行比较。我添加了一个println()用于调试目的,控制台输出就在这个问题的最后。

编辑:我已添加了界面Drawables的代码,以显示intersects(Rectangle rectangle)Shape.getBounds()的曝光方式。我还添加了我的Collisions类来展示它的方法。

编辑2:我无法相信我忽略了提及:drawables是一个集合! drawables.add()的行为与Set.add()

相似

* 编辑8564985294238424: *我从不初始化集合collisions。我修好了。它现在有效。


Class CollisionMonitor (包括问题方法:Set<Collision> getCollisions()这是班级中的最后一个)

package platformer;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;


public class CollisionMonitor
{

private Set<Collision> collisions;
private Set<Drawable> drawables;

public CollisionMonitor(Set<Drawable> drawables)
{
    this.drawables = drawables;
}

public Set<Drawable> getDrawables()
{
    return drawables;
}

public void setDrawables(Collection<? extends Drawable> drawables)
{
    if (drawables != null)
        this.drawables = (new HashSet<Drawable>(drawables));
}

public Set<Drawable> addDrawables(Collection<? extends Drawable> drawables)
{
    if (drawables != null && drawables.isEmpty() == false)
        this.drawables.addAll(drawables);
    return this.drawables;
}

public Set<Collision> getCollisions()    //Problem method!
{
    Set<Drawable> temp = drawables;

    for (Drawable s : drawables)
    {
        for (Drawable ss : temp)
        {
            System.out.println(s + ", " + ss);
            /*if (s.intersects(ss.getBounds()))
            {
                collisions.add(new Collision(s, ss));
            }*/
        }
    }

    return collisions;
}

}

界面可绘制:

public interface Drawable extends Shape
{
    Color color = new Color(0, 0, 0);

    public Color getColor();
}

类碰撞

public class Collision
{

    Drawable drawable1, drawable2;

    public Collision(Drawable drawable1, Drawable drawable2)
    {
        this.drawable1 = drawable1;
        this.drawable2 = drawable2;
    }

}

输出:

platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Bullet@26dea769, platformer.Bullet@26dea769
platformer.Bullet@f1e9457d, platformer.Bullet@f1e9457d
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Bullet@11bd2f5a, platformer.Bullet@11bd2f5a
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Bullet@70a511a, platformer.Bullet@70a511a
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Enemy@660de2f0, platformer.Enemy@660de2f0
platformer.Bullet@6ada16bc, platformer.Bullet@6ada16bc
platformer.Bullet@6ba127e8, platformer.Bullet@6ba127e8

1 个答案:

答案 0 :(得分:0)

tempdrawables没有任何不同。它指向同一个对象。

如果您想要使用完全相同的元素:

for (Drawable s : drawables)
{
    for (Drawable ss : temp)
    {
        if (s == ss) continue;  // don't compare the object to itself