如何打印在arraylist中增加的值?

时间:2018-12-05 16:04:27

标签: java arraylist printing main

我有这段代码,旨在从数组列表中获取double类型的值,并将它们增加一个百分比。

des.cpp

我想在增加之前和之后打印出arraylist中的对象,但是我不确定如何。我知道当前的打印命令不正确,它们是占位符。我尝试了其他方法,例如

public class Company {
static ArrayList<Employee> employees;

public Company() {
    Company.employees = new ArrayList<Employee>();
}


    public ArrayList<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(ArrayList<Employee> employees) {
        Company.employees = employees;
    }

        public void increaseSalaries(double rate) {
            if (rate <= 0.0) {
                throw new IllegalArgumentException();
            } else {
            for (int i = 0 ; i < employees.size() ; i++) {
                employees.get(i).increaseSalaries(rate);
                }
            }
        }

    public static void main(String[] args) {
        Company businesstown = new Company();
        HourlyEmployee george;
        MonthlyEmployee ambrose;
        george = new HourlyEmployee("George", "McClellan", "1537", 1.04);
        ambrose = new MonthlyEmployee("Ambrose", "Burnside", "1536", 741.0);
        businesstown.getEmployees().add(george);
        businesstown.getEmployees().add(ambrose);
        double increase = 20.0;
        System.out.println(businesstown);
        businesstown.increaseSalaries(increase);
        System.out.println(businesstown);

    } 

}

但是这没有用。在这种情况下会起作用吗?

1 个答案:

答案 0 :(得分:0)

您可以对每个循环使用a来打印出数组的所有内容:

for(Employee e: employees){
    System.out.println(e.getSalary());
}