2015-06-22 19 views
0

我创建了一个类employee.it,它有四个变量name,id,salary,city.i创建了一个雇员类对象的数组列表。当我运行该文件时,它只显示defaullt values.But我想在我的数组list.what应该怎么做添加一个以上的价值吗?请帮我解决这个这里是我的代码在数组列表中添加多个值

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(); 
     } 

    } 
} 
+2

那么你想从哪里得到变量的值? –

+2

使用getters&setters – Madhan

回答

0

您已经创建Employee类采用默认值的字段。

您尚未定义任何参数化构造函数或setter来容纳新员工。看到如何做到这一点:

使用构造函数添加员工;迭代和打印员工列表:

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类:

class Employee { 

    private String name; 
    private int id; 
    private String city; 
    private int salary; 

    public Employee(String name, int id, String city, int salary) { 
     this.name = name; 
     this.id = id; 
     this.city = city; 
     this.salary = salary; 
    } 

    // @Override 
    public String toString() { 
     return "Employee: name = " + name + "; Id = " + id + "; City = " + city 
       + "; Salary = " + salary; 
    } 
} 

您还可以定义setter方法[在Employee类]举行的实例变量的值。

+0

你不需要调用'super();'。 – Tom

+1

Eclipse在默认情况下自动生成构造函数...删除了那个 – Rajesh

+0

那么,我希望有一个设置可以转化...或者使用不会产生“噪声”的更好的IDE:D。 – Tom

0

当您需要使用不同的值创建该类的更多对象时,类的默认设置属性不是一个好习惯。相反,

  • 传递在构造函数中的参数的信息时对象的初始化,或

  • 使用setter-getter

撰写您员工settergetter,像这样:

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 =雇员:名称=萨拉; Id = 102;城市= orny;薪酬= 13000; hashCode = 27134973

listOfEmployee(0)= Employee:name = sarah; Id = 102;城市= orny;薪酬= 13000; hashCode = 27134973

emp =员工:姓名=姓名; Id = 12345;城市=城市;薪酬= 123456; hashCode = 1284693

listOfEmployee(1)= Employee:name = Name; Id = 12345;城市= cityName;薪酬= 123456; hashCode = 1284693

+0

thankuu这么多 – USERRR5