[英]How to get total count number of employee in Java?
我在下面有一个Employee和Employees类。 如何在数组列表中的 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.