我将如何创建一个将数组添加到数组列表并调用它们的方法的主要方法?
How would I make a main method which adds arrays to an arraylist and calls a method on them?
我有这个 class 是为了从 ArrayList
中的数组中获取 double
类型的值,并按定义的速率增加它们:
import java.util.ArrayList;
public class Company {
static ArrayList<Employee> employees;
public Company() {
Company.employees = new ArrayList<Employee>();
}
public static ArrayList<Employee> getEmployees() {
return employees;
}
public static 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;
HourlyEmployee george;
MonthlyEmployee ambrose;
george = new HourlyEmployee("George", "McClellan", "1537", 1.04);
ambrose = new MonthlyEmployee("Ambrose", "Burnside", "1536", 741.0);
}
但我不确定如何将这些数组添加到 ArrayList
商业街。我尝试了很多 .add(george);
的组合,但我无法正确组合。要么它说
the method add(HourlyEmployee) is not defined for type Company
或者它编译并抛出 NullPointerException
。
(我应该补充一点,HourlyEmployee
和 MonthlyEmployee
都是 class 扩展 Employee
)
您可以将它们添加到 getEmployees
返回的 List
:
businesstown.getEmployees().add(george);
businesstown.getEmployees().add(ambrose);
但请确保在访问 businesstown
之前初始化实例和其中的 employees
。
Company businesstown = new Company(); // since for you this is initialisin the list as well
在我看来,最好让 ArrayList 成为 class 的私有成员,并有一个用于添加员工的 add 方法
public class Company {
private ArrayList<Employee> employees;
public Company() {
employees = new ArrayList<Employee>();
}
public void addEmployee(Employee e) {
employees.add(e);
}
//rest of code
}
并在主要部分
public static void main(String[] args) {
Company businesstown = new Company();
HourlyEmployee george = new HourlyEmployee("George", "McClellan", "1537", 1.04);
MonthlyEmployee ambrose = new MonthlyEmployee("Ambrose", "Burnside", "1536", 741.0);
businesstown.add(george);
businesstown.add(ambrose);
}
我有这个 class 是为了从 ArrayList
中的数组中获取 double
类型的值,并按定义的速率增加它们:
import java.util.ArrayList;
public class Company {
static ArrayList<Employee> employees;
public Company() {
Company.employees = new ArrayList<Employee>();
}
public static ArrayList<Employee> getEmployees() {
return employees;
}
public static 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;
HourlyEmployee george;
MonthlyEmployee ambrose;
george = new HourlyEmployee("George", "McClellan", "1537", 1.04);
ambrose = new MonthlyEmployee("Ambrose", "Burnside", "1536", 741.0);
}
但我不确定如何将这些数组添加到 ArrayList
商业街。我尝试了很多 .add(george);
的组合,但我无法正确组合。要么它说
the method add(HourlyEmployee) is not defined for type Company
或者它编译并抛出 NullPointerException
。
(我应该补充一点,HourlyEmployee
和 MonthlyEmployee
都是 class 扩展 Employee
)
您可以将它们添加到 getEmployees
返回的 List
:
businesstown.getEmployees().add(george);
businesstown.getEmployees().add(ambrose);
但请确保在访问 businesstown
之前初始化实例和其中的 employees
。
Company businesstown = new Company(); // since for you this is initialisin the list as well
在我看来,最好让 ArrayList 成为 class 的私有成员,并有一个用于添加员工的 add 方法
public class Company {
private ArrayList<Employee> employees;
public Company() {
employees = new ArrayList<Employee>();
}
public void addEmployee(Employee e) {
employees.add(e);
}
//rest of code
}
并在主要部分
public static void main(String[] args) {
Company businesstown = new Company();
HourlyEmployee george = new HourlyEmployee("George", "McClellan", "1537", 1.04);
MonthlyEmployee ambrose = new MonthlyEmployee("Ambrose", "Burnside", "1536", 741.0);
businesstown.add(george);
businesstown.add(ambrose);
}