I am having problems with the copy constructor of a Linkend List in Java.
The list I am trying to copy has a size of 3, when I use the copy constructor the list is empty.
When I try this with the clone method everything works great.
I have look a this for a quite a while and I get the feeling it is so obvious. I just
dont see it, here is the code.
public class Employee {
private String name;
private double salary;
public Employee(String name, double salary){
this.name = name;
this.salary = salary;
}
public void setname(String name){
this.name = name;
}
public void setsalary(double salary){
this.salary = salary;
}
public String getname(){
return this.name;
}
public double getsalary(){
return this.salary;
}
}
public class Main {
public static void main(String[] args) {
Employees employees = new Employees();
employees.add(new Employee("Employee1", 2500.00));
employees.add(new Employee("Employee2", 2400.00));
employees.add(new Employee("Employee3", 2000.00));
Employees employeesCopy2 = new Employees(employees);
Employees employeesCopy = (Employees) employees.clone();
System.out.println(employees.size());
System.out.println(employeesCopy2.size());
System.out.println(employeesCopy.size());
}
}
import java.util.LinkedList;
public class Employees extends LinkedList<Employee> {
private static final long serialVersionUID = 1L;
private LinkedList<Employee> employees;
public Employees(){
employees = new LinkedList<Employee>();
}
public Employees(Employees w){
employees = new LinkedList<Employee>(w);
}
public void addWerknemer(Employee w){
employees.add(w);
}
}
EDIT
This is homework, but when I wanted to add the tag is showed that the tag was no longer is use.
Your confusion comes from the fact, that
Employeesboth is a list and contains a list. When you useyou add the employee to the outer list. When you use
you add the employee to the inner list. Since you have overwritten the constructor
Employees(Employees es), this will not clone the outer list, but only the inner. And since you haven’t overwrittenclone(), it will clone the outer list, but not the inner. This is rather messy and also most probably not intended by you. I therefore propose one of the following changes:1. [Preferred] Employees only contains a list and not extends one
Skip the
extends LinkedList<Employee>and only work with an internal list. You will have to use your methodaddWerknemer(Employee emp)to add to your list (or change it’s name toadd). You will have to implementsizeandcloneas well as other methods that you wish to use. If you want to be really clean about this, you can even make the classimplement Listorimplement Collectionor so. This way you can still treat your class as ajava.util.Collection. I don’t think that this would be neccessary in your case though. Also you would need to implement all of the interfaces methods (there are many). An example implementation would look like this. You still have to implementsize, etc.2. Employees only extends
LinkedListand doesn’t contain oneThrow away your methods
addWerknemer(Employee emp)and the copy constructorEmployees(Employees)as well as your internal list. This way you will not overwrite the existing implementations ofLinkedList. This approach is more or less useless because you basically just renameLinkedListtoEmployeesand add/change nothing. Therefore I wouldn’t recommend this approach.