ArrayList
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: ArrayList

  1. #1
    Join Date
    May 2009
    Posts
    11

    ArrayList

    Hi,

    How can I add this class object to an arraylist so that it can store a list of employee's details. And my main program able to call that function to retrieve the list of data.
    Thanks

    Code:
    public class Employee {
      private String lastName;
    
      private String firstName;
    
      public Employee(String lastName, String firstName) {
        this.lastName = lastName;
        this.firstName = firstName;
      }
     
    public String getLastName() {
        return this.lastName;
      }
    
      public void setLastName(String lastName) {
    
        this.lastName = lastName;
      }
    
      public String getFirstName() {
        return this.firstName;
      }
    
      public void setFirstName(String firstName) {
        this.firstName = firstName;
      }
    
      public Object clone() {
        Employee emp;
        emp = new Employee(this.lastName, this.firstName);
        emp.setSalary(this.salary);
        return emp;
      }
    
    }
    Last edited by part0; August 14th, 2011 at 10:11 PM.

  2. #2
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    362

    Re: ArrayList

    Please, re-edit you post and add code tags.

    Just create an ArrayList to store your objects:
    Code:
    public class myClass {
       // Create ArrayList
       List<Employee> employees = new ArrayList<Employee>();
    
       // Create as many Employee objects as you need
       Employee emp1 = new Employee ("lastName1", "firstName1");
       Employee emp2 = new Employee ("lastName2", "firstName2");
       [...] 
    
       // Store into array
       employees.add(emp1);
       employees.add(emp2);
       [...]
    
       // You have an ArrayList of employees in employees object.
    }
    Hope this helps.

    Regards,

    Albert.
    Please, correct me. I'm just learning.... and sorry for my english :-)

  3. #3
    Join Date
    May 2009
    Posts
    11

    Re: ArrayList

    Thanks for the reply.

    But is there a way to put the arraylist in the object class empolyee method?

    So that whenever my main class calls the object class empolyee it will just auto add in to the arraylist.

    As I dont whish to initialize like this...

    // Create as many Employee objects as you need
    Employee emp1 = new Employee ("lastName1", "firstName1");
    Employee emp2 = new Employee ("lastName2", "firstName2");
    [...]

    As this will become hardcoded... you will never know how many empolyees needed to be initialize... assume that there are a lot of employees and the above method might not be a good choice to do it.

    Pls assist again thanks.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: ArrayList

    You shouldn't put the collection of Employees in the Employee class.

    An Employee is a single employee, if you want to keep a record of all employees you need to a have collection class (ie ArrayList) in whatever class adds and removes (hires and fires) employees.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    362

    Re: ArrayList

    Quote Originally Posted by part0 View Post
    Thanks for the reply.

    But is there a way to put the arraylist in the object class employee method?
    As keang told you, I'd never put the List inside element class, unless there's no other option. Always try to make your program as much simple as possible. It'll be easy to understand and to correct.
    So that whenever my main class calls the object class empolyee it will just auto add in to the arraylist.
    I insist not to do that, but if that is what you need there are several options:
    • Create List in main class and pass it as an argument to constructor in Employee Class, ie:
      In main class
      Code:
      List<Employee> employees = new ArrayList<Employee>(); // Create List
      
      //for each employee: 
      Employee emp1 = new Employee("firstName", "lastName", employees);
      [....]
      In Employee class:
      Code:
      //Constructor
      public Employee(String firstName, String lastName, List<Employee> employees) {
         this.firstName = firstName;
         this.lastName = lastName;
         employees.add(this);
      }
    • Make list static and public (or private with statics getter and setter).
      In main class:
      Code:
      public class MainClass {
         public static List<Employee> employees = new ArrayList<Employee>();
         [...] // Create each your employee object
      }
      In Employee class:
      Code:
      //Constructor
      public Employee(String firstName, String lastName) {
         this.firstName = firstName;
         this.lastName = lastName;
         MainClass.employees.add(this); // static access 
      }
      In this case, you must be careful if there are several threads: The access should be synchronized.

    As I dont whish to initialize like this...

    // Create as many Employee objects as you need
    Employee emp1 = new Employee ("lastName1", "firstName1");
    Employee emp2 = new Employee ("lastName2", "firstName2");
    [...]

    As this will become hardcoded... you will never know how many empolyees needed to be initialize... assume that there are a lot of employees and the above method might not be a good choice to do it.
    It doesn't matter. It was just an example. Nothing changes, but the way you create each Employee object: you can read them from a file or a database, or from whatever you want.
    There is no need to know how many employees are for ArrayList. It'll increase its own size when needed: Don't worry about that.

    Regards,

    Albert.
    Please, correct me. I'm just learning.... and sorry for my english :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center