4
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.

2
  • You need to get your programming concepts clear. Refer @RamPrakash 's answer. Commented Dec 22, 2019 at 4:46
  • You need to use single object to add and retrieve the list from the arraylist Commented Dec 22, 2019 at 6:13

3 Answers 3

3

You haven't added any employee instances into your EmployeeDB before listing all employees in it. Instead you'd added them to a different EmployeeDB instances. Try the snippet below.

public static void main(String[] args) {
        System.out.println("  ");

        Employee e1 = new Employee(111,"Employee-1","loc-1",100);
        Employee e2 = new Employee(222,"Employee-2","loc-2",200);

        EmployeeDB x = new EmployeeDB();
        x.addEmployee(e1);
        x.addEmployee(e2);
        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("**************");
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

2

You are adding Employee object to new EmployeeDB objects every time. Instead of adding Employee Object to existing EmployeeDB Object.

EmployeeDB object x has to be used to add employee object and also to retrieve added employee objects x.listAll()

public class Main
    {
        public static void main(String[] args) {
            System.out.println("  ");

            EmployeeDB x = new EmployeeDB();

            Employee e1 = new Employee(111,"Employee-1","loc-1",100);
            //EmployeeDB d1 = new EmployeeDB();
            x.addEmployee(e1);


            Employee e2 = new Employee(222,"Employee-2","loc-2",200);
          //  EmployeeDB d2 = new EmployeeDB();
            x.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("**************");
            }
        }
    }

Comments

1

It seems you are using public objects instead of arrays. It would be much simpler to just create an array of objects. That way you could properly check attributes and list size. You wouldn't need all this complex, messy code and pointers and .thises. This would also let you use JSON if you need to easily store your databases.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.