如何获取 Java 中的员工总数?

How to get total count number of employee in Java?

我下面有 EmployeeEmployees class。 如何返回数组列表中第 n 个位置的 Employee 对象?

//Employee.java
public class Employee {
    private String name;
    private String email;
    
    public Employee (String name, String email) {
        this.name = name;
        this.email = email;
        
    }
//Employees.java
import java.util.ArrayList;

public class Employees {

    private ArrayList<Employee> employees;
    
    public Employees(){
        this.employees = new ArrayList<Employee>();
        employees.add(new Employee("Thomas Muller","thomas.muller@uts.com"));
    }
}

您可以定义一个简单的getCount()方法来获取员工总数:

public class Employees {
...
  public int getCount() {
    return this.employees.size();
  }
}

稍后您可以在 Employees 实例上调用它:

Employees employees = new Employees();
int totalCount = employees.getCount();
System.out.println("Total count of employees:" + totalCount);