2015-06-22 42 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