artist
artist

Reputation: 39

storing objects in arraylist and returning those objects

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.

Upvotes: 4

Views: 79

Answers (3)

RamPrakash
RamPrakash

Reputation: 1834

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

Upvotes: 2

amyiris
amyiris

Reputation: 113

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.

Upvotes: 1

Subramanian Mariappan
Subramanian Mariappan

Reputation: 3886

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

Upvotes: 3

Related Questions