【问题标题】:How to add ArrayList parameter to an ArrayList then then add to current class ArrayList如何将 ArrayList 参数添加到 ArrayList 然后添加到当前类 ArrayList
【发布时间】:2020-11-08 06:16:42
【问题描述】:

我有一个名为 Service 的 ArrayList,它接受一个枚举 ServiceType、一个双倍价格和一个 ArrayList Employee。

我正在尝试创建一种将服务元素添加到 Service ArrayList 的方法,但我收到一条错误消息

Service 中的Service() 不能应用于预期参数实际参数 服务名称:服务类型服务名称 price double price new Employee(mechanicName) (Employee)

我尝试在 Service ArrayList 对象中为 Employee 创建 ArrayList 对象,然后将其添加到 Transactions ArrayList 中,但这不起作用。

///事务类

public class Transaction {
    private ArrayList<Service> services;

    public Transaction() {
        this.services = new ArrayList<Service>();
    }

    
    public boolean createNewService(ServiceType serviceName, double price,
                                    Employee mechanic, String mechanicName){
        Service existingService = findService(serviceName, price, mechanic);
        if(existingService == null){

            this.services.add(new Service(serviceName, price, new Employee(mechanicName)));
        }
        return false;
    }

    
    private Service findService(ServiceType serviceName, double price,
                                Employee machanic){
        for(int i=0; i<this.services.size(); i++){
            Service chkdServise = this.services.get(i);
            if(this.services.get(i).getServiceName().equals(serviceName) &&
            this.services.get(i).getPrice() == price &&
            this.services.get(i).getMachanics().equals(machanic)){
                return chkdServise;
            }
        }
        return null;
    }

///服务类

public class Service {
    private ServiceType serviceName;
    private double price; 
    private ArrayList<Employee> machanics;


    public Service(ServiceType serviceName, double price) {
        this.serviceName = serviceName;
        this.price = price;
        this.machanics = new ArrayList<Employee>();
    }

public boolean createEmployee(String name){
        Employee existingEmployee = findEmployee(name);
        if(existingEmployee == null){
            this.machanics.add(new Employee(name));
            return true;
        }
        return false;
    }


    private Employee findEmployee(String name){
        for(int i=0; i<machanics.size(); i++){
            Employee chkedEmployee = this.machanics.get(i);
            if(this.machanics.get(i).getName().equals(name)){
                return chkedEmployee;
            }

        }
        return null;
    }

///员工类

public class Employee {
    private String name;
    private int salary;


    public Employee(String name) {
        this.name = name;
        this.salary = 50000;
    }

    public String getName() {
        return name;
    }

    public int getSalary() {
        return salary;
    }


    }

【问题讨论】:

  • 能否显示完整的错误堆栈跟踪。 ?

标签: java object arraylist


【解决方案1】:

您的代码存在一些问题。但导致错误的最重要的一个是,您的Service 有一个构造函数,它只接受两个参数ServiceTypeprice,但您试图在Transaction 类中传递三个参数。你需要的是:

    public boolean createNewService(ServiceType serviceName, double price,
                                    Employee mechanic, String mechanicName){
        Service existingService = findService(serviceName, price, mechanic);
        if(existingService == null){
            Service newService = new Service(serviceName, price);
            newService.createEmployee(new Employee(mechanicName));
            this.services.add(newService);
        }
        return false;
    }

您看到我更改了代码,只为 Service 对象传递了两个参数,这是其构造函数所需要的。然后使用您在Service 类中已有的createEmployee 方法。然后将新服务添加到服务列表中。

其他小的改进(我可以找到):

  1. findService方法
private Service findService(ServiceType serviceName, double price,
                            Employee machanic) {
    for(int i=0; i<this.services.size(); i++) {
        Service chkdServise = this.services.get(i);
        if(chkdServise.getServiceName().equals(serviceName) &&
        chkdServise.getPrice() == price &&
        chkdServise.getMachanics().equals(machanic)){
            return chkdServise;
        }
    }
    return null;
}

请注意,您不需要每次都使用this.services.get(i)。您已经在chkdServise 中获得过一次。

  1. findEmployee 类似的改进:
private Employee findEmployee(String name){
        for(int i=0; i<machanics.size(); i++){
            Employee chkedEmployee = this.machanics.get(i);
            if(chkedEmployee.getName().equals(name)){
                return chkedEmployee;
            }

        }
        return null;
    }

【讨论】:

    【解决方案2】:

    Service 类的构造函数参数不匹配。用的是什么IDE?您应该在编译步骤中遇到了错误。

    【讨论】:

    • 我正在使用 Intellij。谢谢,我可能确实得到了一个,但我正在学习 java,我认为我不理解所显示的错误。
    猜你喜欢
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    • 1970-01-01
    相关资源
    最近更新 更多