【问题标题】:How do i iterate through my ArrayList of objects to create a condition?如何遍历对象的 ArrayList 以创建条件?
【发布时间】:2017-08-12 05:06:34
【问题描述】:

我试图让我的最后一个 IF 循环遍历其中的三个对象 PinballTypeOne 列表中,只有 ArrayList 中的这三个对象在撞到左墙后才应将其原来的颜色更改为橙​​色。碰撞确实有效,但对象会不断反弹而不会变为橙色,所以我认为我可能对 ArrayList 的制作方式或我如何尝试遍历 ArrayList 有问题。没有编译错误。

   public class PinballObject
{
    private int currentXLocation;
    private int currentYLocation;
    private int speedXTravel;
    private int speedYTravel;
    private Color colour;
    private int radius;
    private Machine machine;
    private final int leftWallPosition;
    private final int bottomWallPosition;
    private final int topWallPosition;
    private final int rightWallPosition;
    private ArrayList<PinballObject> PinballTypeOneList = new ArrayList<PinballObject>();




    /**
     * Constructor for objects of class Pinball_Obj
     * 
     * @param xPos  the horizontal coordinate of the object
     * @param yPos  the vertical coordinate of the object
     * @param xVel  the horizontal speed of the object
     * @param yVel  the vertical speed of the object
     * @param objectRadius  the radius (in pixels) of the object
     * @param objectColor  the color of the object
     * @param theMachine  the machine this object is in
     */
    public PinballObject(int xPos, int yPos, int xVel, int yVel, Color objectColor, int objectRadius, Machine theMachine)
    {
        currentXLocation = xPos;
        currentYLocation = yPos;
        speedXTravel = xVel;
        speedYTravel = yVel;
        colour = objectColor;
        radius = objectRadius;
        machine = theMachine;
        leftWallPosition = machine.getLeftWall();
        bottomWallPosition = machine.getBottomWall();
        topWallPosition = machine.getTopWall();
        rightWallPosition = machine.getRightWall();
    }

    public void makeType1()
    {
        PinballObject firstTypeOne = new PinballObject(50, 200, -5, 3, Color.RED, 10, machine);
        PinballTypeOneList.add(firstTypeOne);

        PinballObject secondTypeOne = new PinballObject(100, 300, 1, 2, Color.BLUE, 55, machine);
        PinballTypeOneList.add(secondTypeOne);

        PinballObject thirdTypeOne = new PinballObject(450, 125, -1, -1, Color.YELLOW, 40, machine);
        PinballTypeOneList.add(thirdTypeOne);
    }


    /**
     * Move this object according to its position and speed and redraw.
     **/
    public void move()
    {
         // remove from universe at the current position
        machine.erase(this);

        // compute new position
        currentYLocation += speedYTravel;
        currentXLocation += speedXTravel;

        // check if it has hit the leftwall + change color to orange only for the objects IN the arraylist
        if(currentXLocation <= (leftWallPosition + radius)) 
        {
            currentXLocation = leftWallPosition + radius;
            speedXTravel = -speedXTravel;

            if(this.equals(PinballTypeOneList)){
                colour = Color.ORANGE;
            }

            // draw again at new position
         machine.draw(this);

        }
    }

【问题讨论】:

  • 您的 if (this.equals(PinballTypeOneList)) 将始终返回 true。
  • 这是什么意思?
  • 这意味着它在反弹后总是橙色的。这也意味着你在代码中有无用的if
  • 问题是它们根本不会变成橙色,如果我删除条件,我可以让它们全部变成橙色,但我想要做的只是让 ArrayList 中的对象变成橘子。它们在碰撞后反弹相同的颜色。正如我上面提到的,我认为我可能对 ArrayList 的制作方式或尝试遍历 ArrayList 的方式有疑问,我要么是盲人,要么就是看不到它。
  • 首先你有一个类PinballObject,它有一个构造函数和函数move()makeType1()this 返回PinballObject 类,它不能等于PinballTypeOneList 这是PinballObject 的数组,所以你需要select 以某种方式从数组中选择一个PinballObject 并使用你的@987654332 @。 if (this.equals(PinballTypeOneList)) 总是返回 false。

标签: java arraylist bluej


【解决方案1】:

对于您的功能移动,您应该这样做:

public void move()
{
    machine.erase(this);

    currentYLocation += speedYTravel;
    currentXLocation += speedXTravel;

    if(currentXLocation <= (leftWallPosition + radius)) 
    {
        currentXLocation = leftWallPosition + radius;
        speedXTravel = -speedXTravel;
        for(PinballObject po : PinballTypeOneList) { //Iterating through list of PinballObject's
            if(this.equals(po)) { // If this PinballObject is in list
                colour = Color.ORANGE; // Or use po.colour
            }
        }
        machine.draw(this);
    }
}

并且您需要覆盖 equalshashCode 方法,因为您使用自己定义的类(PinballObject),请参阅this question

我对您的 PinballTypeOneList 有疑问,为什么它在 PinballObject 类中?这意味着每个PinballObject 都有它,这听起来很糟糕...您需要将 PinballTypeOneList 设为全局,然后将您的PinballObjects 存储在其中,或者您有特殊的理由这样做?

在此之后,我相信您的代码应该可以工作。(虽然我没有看到大多数代码)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    • 2015-07-22
    • 2019-04-19
    • 2016-01-12
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多