我正在尝试在列表employ
上存储一组对象。但是我在employ.addAll()
收到错误,我尝试employ.add()
但我仍然收到错误。
import java.util.ArrayList;
public class Employee {
public String FullName;
public float wage;
public int ID;
ArrayList<Employee> employ = new ArrayList<Employee>();
Employee(String name, float wage, int ID){
this.FullName = name;
this.wage = wage;
this.ID = ID;
}
Employee e = new Employee("Tony", 1245, 2222);
employ.addAll(e); //here is where I a getting the error
}
答案 0 :(得分:1)
您要添加一个Employee
,因此请使用add
,而不是addAll
:
Employee e = new Employee("Tony", 1245, 2222);
employ.add(e);
除此之外,employ.add(e);
应该在某个方法中。
答案 1 :(得分:0)
employ.addAll()
需要一个集合,用于添加单个元素employ.add()
。
import java.util.ArrayList;
public class Employee {
public String FullName;
public float wage;
public int ID;
Employee(String name, float wage, int ID){
this.FullName = name;
this.wage = wage;
this.ID = ID;
}
public static void main(String[] args) {
ArrayList<Employee> employ = new ArrayList<Employee>();
Employee e = new Employee("Tony", 1245, 2222);
employ.add(e);
}
}