-
August 12th, 2011, 02:48 AM
#1
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.
-
August 12th, 2011, 06:24 AM
#2
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 :-)
-
August 14th, 2011, 10:07 PM
#3
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.
-
August 15th, 2011, 04:48 AM
#4
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.
-
August 16th, 2011, 07:38 AM
#5
Re: ArrayList
Originally Posted by part0
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|