簡體   English   中英

在數組列表中添加多個值

[英]add multiple value in array list

我創建了一個employee類。它具有四個變量名稱,id,salary,city。我創建了一個employee class object的數組列表。當我運行該文件時,它僅顯示默認值。但是我想添加多個我的數組列表中的一個值。我該怎么辦?請幫我解決這個問題,這是我的代碼

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

    }
}

您已使用字段的默認值創建了Employee類。

您尚未定義任何參數化的構造函數或設置方法來容納新Employee。 了解如何做到這一點:

使用構造函數添加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類中添加參數化的構造函數:

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

您還可以定義[在Employee類中] setter方法來保存實例變量的值。

當您需要使用不同的值創建該類的更多對象時,默認設置類的屬性不是一個好習慣。 而是

  • 對象初始化時將信息傳遞給構造函數參數,或者

  • 使用setter-getter

settergetter編寫您的Employee類,如下所示:

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; 城市=正常; 薪水= 13000; hashCode = 27134973

listOfEmployee(0)=員工:名稱= sarah; Id = 102; 城市=正常; 薪水= 13000; hashCode = 27134973

emp =雇員:名稱=名稱; ID = 12345; 城市=城市; 薪水= 123456; hashCode = 1284693

listOfEmployee(1)=員工:名稱=名稱; ID = 12345; 城市= cityName; 薪水= 123456; hashCode = 1284693

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM