简体   繁体   English

将对象存储在 arraylist 中并返回这些对象

[英]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 .在这里,我将两个对象添加到 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.如果在添加两个对象后它告诉我数组列表的大小为 0. 并且不显示任何元素。

so please help me to know how can i get the desire result.所以请帮助我知道我怎样才能得到想要的结果。

You haven't added any employee instances into your EmployeeDB before listing all employees in it.在列出所有员工之前,您尚未将任何员工实例添加到您的EmployeeDB中。 Instead you'd added them to a different EmployeeDB instances.相反,您将它们添加到不同的 EmployeeDB 实例。 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("**************");
        }
    }

You are adding Employee object to new EmployeeDB objects every time.您每次都将Employee对象添加到新的EmployeeDB对象中。 Instead of adding Employee Object to existing EmployeeDB Object.而不是将Employee对象添加到现有的EmployeeDB对象中。

EmployeeDB object x has to be used to add employee object and also to retrieve added employee objects x.listAll() EmployeeDB对象x必须用于添加员工对象并检索添加的员工对象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("**************");
            }
        }
    }

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 .this es.您不需要所有这些复杂、凌乱的代码和指针以及.this es。 This would also let you use JSON if you need to easily store your databases.如果您需要轻松存储数据库,这也可以让您使用 JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM