表达式的类型必须是数组类型,但它解析为int错误

时间:2014-06-16 17:24:40

标签: java arrays

我想为一组数组编译一个白色循环,但是收到以下错误:

  

表达式的类型必须是数组类型,但它已解析为int

这就是我所拥有的:

package PayrollPractice;
import java.text.DecimalFormat;

//Payroll Class, pg 504

public class Payroll 
{
    private int[] _emp;         //employee ID
    private int[] _hours;           //hours worked
    private double[] _rates;        //rate of pay
    private double[] _wages;        //wages

    public Payroll() 
    {
        //array of employee ID(s)
        _emp = new int[] { 5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 7580489 };
    }

    public boolean is_emp(int number)
    {
        int index,          //loop control variable
            _emp;           //element the value is found at
        boolean found;      //flag indicating the search results

        //element 0 is the starting point of the search, pg 463
        index = 0;

        //Store the default values for the element and found, pg 463
        _emp = -1;
        found = false;

        //search the array, pg 463
        while (!found && index < _emp)
        {
            //does this element have a value?
            if (_emp[index] == number)       //ERROR: The type of the expression must be an array type but it resolved to int
            {
                found = true;
            }
            else {
                //increment index so we can look at the next element
                index++;
            }

        }

        //return either the subscript of the value(if found)
        //or -1 to indicate the value was not found.
        return found;   
}

4 个答案:

答案 0 :(得分:3)

你有一个名为_emp的顶级变量,它是一个数组。

然后通过在方法中使用另一个具有相同名称的变量隐藏该变量。该变量是一个int。重命名您的一个变量,或使用 this 关键字明确引用顶级变量。

答案 1 :(得分:1)

程序中有两个名为_emp的变量。一个是类型数组的类字段,另一个是方法内部类型为int的局部变量。如果要访问类型数组,则需要使用this._emp this引用类对象。

答案 2 :(得分:0)

你宣布:

private int[] _emp; 

并指定:

_emp = -1;

那不对,是吗?

请丢失那个糟糕的C ++下划线来表示私人类成员。看起来很糟糕的代码。

我是这样做的:

public class Payroll {
    private static final int[] DEFAULT_EMPLOYEES = {5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 7580489};

    private int[] emp;             //employee ID

    public static void main(String[] args) {
        Payroll payroll = new Payroll();
        for (String arg : args)  {
            System.out.println(String.format("employee ID: %s is employee? %b", arg, payroll.isEmployee(Integer.parseInt(arg))));
        }
    }

    public Payroll() {
        this(DEFAULT_EMPLOYEES);
    }

    public Payroll(int[] emp) {
        if (emp != null) {
            this.emp = new int[emp.length];
            System.arraycopy(emp, 0, this.emp, 0, emp.length);
        }
        this.emp = emp;
    }

    public boolean isEmployee(int number) {
        int index = 0;
        while (index < this.emp.length) {
            if (this.emp[index] == number) {
                return true;
            }
            ++index;
        }
        return false;
    }
}

在你学习的时候,还有别的想法。而不是多个数组,将所有信息封装到Employee类中。然后有一个List或Employees数组。

public class Employee {
    private int number;
    private int hoursWorked;
    private double payPerHour;

    public Employee(int n, int h, double r) {
        this.number = n;
        this.hoursWorked = h;  // negative allowed?  I'd guess "no"
        this.rate = r;  // negative allowed?  I'd guess "no"
    }

    public double getWages() { 
        return this.hoursWorked*this.payPerHour;  // don't set it; calculate it
    }
}

答案 3 :(得分:0)

你的构造函数也没有关闭;你需要一个}。

相关问题