import java.util.*;
import java.util.ArrayList;
class Employee{
int eid;
String eName;
String eAddr;
int eSalary;
public Employee(int eid,String eName,String eAddr,int eSalary){
this.eid = eid;
this.eName = eName;
this.eAddr = eAddr;
this.eSalary=eSalary;
}
}
class EmployeeDB{
ArrayList<Employee> al = new ArrayList<Employee>();
public boolean addEmployee(Employee e){
return al.add(e);
}
public Employee[] listAll() {
Employee[] array = new Employee[al.size()];
System.out.println("SIZE is : "+al.size());
return al.toArray(array);
}
}
public class Main
{
public static void main(String[] args) {
System.out.println(" ");
Employee e1 = new Employee(111,"Employee-1","loc-1",100);
EmployeeDB d1 = new EmployeeDB();
d1.addEmployee(e1);
Employee e2 = new Employee(222,"Employee-2","loc-2",200);
EmployeeDB d2 = new EmployeeDB();
d2.addEmployee(e2);
EmployeeDB x = new EmployeeDB();
Employee[] arr = x.listAll();
for(Employee e : arr){
System.out.println( e.eid );
System.out.println( e.eName );
System.out.println( e.eAddr );
System.out.println("**************");
}
}
}
Here i am adding two object to the arraylist . and in the end i try to display this both element in main class function.
but the element is not getting added. in case after adding the two objects its telling me that the size of the arraylist is 0. and not displaying any elements.
so please help me to know how can i get the desire result.