Java – List of Employee objects from Array

Employee.java:

  • Create Employee class with instance variables id, name, salary
  • Define parameterized constructor to initialize the object.
class Employee
{
            int id;
            String name;
            double salary;
            Employee(int id, String name, double salary) {
                        this.id = id;
                        this.name = name;
                        this.salary = salary;
            }
}

Main.java:

  • Collect values from Arrays to create Employee objects.
  • Display details using for-each loop
import java.util.*;
class Main
{
            public static void main(String[] args) {
                        int[] ids = {101, 102, 103, 104, 105};
                        String[] names = {“Amar”, “Annie”, “Harini”, “Satya”, “Jai”};
                        double[] salaries = {23000, 56000, 43000, 48000, 16000};
 
                        List<Employee> list = new ArrayList<Employee>();
                        for (int i=0 ; i<=ids.length-1 ; i++){
                                    Employee e = new Employee(ids[i], names[i], salaries[i]);
                                    list.add(e);
                        }
 
                        System.out.println(“Details are : “);
                        for(Employee e : list){
                                    System.out.println(e.id + ” , ” + e.name + ” , ” + e.salary);
                        }
            }
}
Scroll to Top