簡體   English   中英

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

[英]How do I print the values that have been increased within an arraylist?

我有這段代碼,旨在從arraylist中獲取double類型的值,並將它們增加一個百分比。

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);

    } 

}

我想在增加之前和之后打印出arraylist中的對象,但不確定如何。 我知道當前的打印命令不正確,它們是占位符。 我嘗試了其他方法,例如

for (Object[] array : list) {
    System.out.println(Arrays.toString(array));
}

但這沒有用。 在這種情況下什么工作?

您可以為每個循環使用a打印出數組的所有內容:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM