使用迭代器错误

时间:2015-12-02 06:52:26

标签: java arraylist iterator

我正在使用下面的方法,这将为我提供计算有多少辆车有后轮驱动。数组列表将其存储为布尔值。

当我尝试编译获取无法找到符号的错误时 - 变量getIsRearWheelDrive.I在我的兰博基尼类中有这个方法。

public int howManyAreRearWheelDrive()
    {
        int i = 0;
        Iterator<Lamborghini> rear = inventory.iterator();
        while(rear.hasNext()){
            Lamborghini nextLamb = rear.next();
            if(nextLamb.getIsRearWheelDrive.equals(true))
            {               
                int rearHold = nextLamb.getIsRearWheelDrive();
                i++;
            }  
            return rearHold;
        }

Lamborghini class
public boolean getIsRearWheelDrive()
    {
        return isRearWheelDrive;
    }    

2 个答案:

答案 0 :(得分:0)

尝试更改此行:

 if(nextLamb.getIsRearWheelDrive.equals(true))

这一行:

 if(nextLamb.getIsRearWheelDrive())

因为方法getIsRearWheelDrive()返回一个布尔值 并创建变量isRearWheelDrive

答案 1 :(得分:0)

此代码中有很多错误,我会在其中添加注释

public int howManyAreRearWheelDrive()
{
    int i = 0;
    Iterator<Lamborghini> rear = inventory.iterator();
    while(rear.hasNext()){
        Lamborghini nextLamb = rear.next();

        if(nextLamb.getIsRearWheelDrive ()) // this is a method .equals(true))

        {               
            // int rearHold = nextLamb.getIsRearWheelDrive();
            // why cast this to an local int??
            i++;
        }  
        // return rearHold;
        // readHold is not in scope here
        // if you return here, the while loop will not complete
        // the variable i is hold the value you want
    }

//Lamborghini class
public boolean getIsRearWheelDrive()
{
    return isRearWheelDrive;
}