我创建了一个类employee.it有四个变量名称,id,salary,city.i已经创建了一个雇员类对象的数组列表。当我运行该文件时,它只显示默认值。但我想要在我的数组列表中添加多个值。我该怎么办?请帮我解决这个问题,这是我的代码
import java.util.*;
public class arraylist {
public static void main(String[] args) {
new arraylist();
}
public arraylist() {
List<Employee > listOfEmp = new ArrayList<Employee >();
Employee bk1 = new Employee ();
listOfEmp .add(bk1);
System.out.println(" emp = " + bk1);
System.out.println("listOfEmployee(0) = " + listOfEmp.get(0));
}
public class Employee {
String name="sarah";
int id =102;
String city="orny";
int salary=13000;
// @Override
public String toString() {
return "Employee: name = " + name + "; Id = " + id + "; City = " + city + "; Salary = " + salary + "; hashCode = " + hashCode();
}
}
}
答案 0 :(得分:1)
您已使用字段的默认值创建了Employee类。
您还没有定义任何参数化的构造函数或setter来保存新的Employee。看看如何做到这一点:
使用构造函数添加Employee;迭代并打印员工名单:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if([self checkForSelectedRow:indexPath]){
//change the title of button
}
else{
//default of button
}
在Employee类中添加参数化构造函数:
List<Employee> listOfEmp = new ArrayList<Employee>();
// Add employee to list
listOfEmp.add(new Employee("sarah1", 101, "orny", 13000));
listOfEmp.add(new Employee("sarah2", 102, "orny", 13000));
listOfEmp.add(new Employee("sarah3", 103, "orny", 13000));
// Iterate and print employee list
for (Employee employee : listOfEmp)
System.out.println(employee);
您还可以在[Employee类]中定义setter方法来保存实例变量的值。
答案 1 :(得分:0)
当您需要使用不同的值创建该类的更多对象时,类的默认设置属性不是一个好习惯。相反,
在对象初始化时传递构造函数参数中的信息,或
使用setter-getter
使用setter
和getter
撰写您的员工课程,如下所示:
public class Employee {
String name;
int id;
String city;
int salary;
public Employee() {
// do something if u want
}
public Employee(String name, int id, String city, int salary) {
this.name = name;
this.id = id;
this.city = city;
this.salary = salary;
}
public String toString() {
return "Employee: name = " + name + "; Id = " + id + "; City = " + city
+ "; Salary = " + salary + "; hashCode = " + hashCode();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
然后您可以轻松地轻松添加更多员工,
import java.util.*;
public class Arraylist {
public static void main(String[] args) {
new Arraylist();
}
public Arraylist() {
List<Employee> listOfEmp = new ArrayList<Employee>();
Employee bk2 = new Employee(); //will set attribute later
Employee bk1 = new Employee("sarah", 102, "orny",13000);// attributes set here
listOfEmp.add(bk1);
bk2.setCity("City");
bk2.setId(12345);
bk2.setName("Name");
bk2.setSalary(123456);
listOfEmp.add(bk2);
System.out.println(" emp = " + bk1);
System.out.println("listOfEmployee(0) = " + listOfEmp.get(0));
System.out.println(" emp = " + bk2);
System.out.println("listOfEmployee(1) = " + listOfEmp.get(1));
}
}
目前,我得到以下输出:
emp =员工:name = sarah; Id = 102; City = orny;薪水= 13000; hashCode = 27134973
listOfEmployee(0)=员工:name = sarah; Id = 102; City = orny;薪水= 13000; hashCode = 27134973
emp =员工:name = Name; Id = 12345;城市=城市;薪水= 123456; hashCode = 1284693
listOfEmployee(1)=员工:name = Name; Id = 12345; City = cityName;薪水= 123456; hashCode = 1284693