USERRR5
USERRR5

Reputation: 430

add multiple value in array list

i have created a class employee.it has four variables name,id,salary,city.i have made a array list of the employee class object.When i run the file it only shows the defaullt values.But i want to add more than one value in my array list.what should i do?please help me to solve this here is my code

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

    }
}

Upvotes: 1

Views: 19332

Answers (2)

mazhar islam
mazhar islam

Reputation: 5619

Default setting attributes of a class is not a good practice, when you need to create more object of that class with different values. Rather,

  • Pass the information in the constructor parameter when object initialization, or

  • Use setter-getter

Write your Employee class with setter and getter, like this:

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

}

Then you can easily add more Employee easily,

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

    }
}

Currently, I get the following output:

emp = Employee: name = sarah; Id = 102; City = orny; Salary = 13000; hashCode = 27134973

listOfEmployee(0) = Employee: name = sarah; Id = 102; City = orny; Salary = 13000; hashCode = 27134973

emp = Employee: name = Name; Id = 12345; City = City; Salary = 123456; hashCode = 1284693

listOfEmployee(1) = Employee: name = Name; Id = 12345; City = cityName; Salary = 123456; hashCode = 1284693

Upvotes: 1

Rajesh
Rajesh

Reputation: 2155

You have created Employee class with default values for fields.

You haven't defined any parametrized constructor or setters to hold new Employee. See how to do that:

Add Employee using constructor; Iterate and print employee list:

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

Add parametrized constructor in Employee class:

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

You can also define setter methods[in Employee class] to hold values for instance variables.

Upvotes: 3

Related Questions