Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8937511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:25:45+00:00 2026-06-15T10:25:45+00:00

I am having problems with the copy constructor of a Linkend List in Java.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:25:46+00:00Added an answer on June 15, 2026 at 10:25 am

    Your confusion comes from the fact, that Employees both is a list and contains a list. When you use

    employees.add(new Employee("Employee1", 2500.00));
    

    you add the employee to the outer list. When you use

    employees.addWerknemer(new Employee("Employee1", 2500.00));
    

    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 overwritten clone(), 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 method addWerknemer(Employee emp) to add to your list (or change it’s name to add). You will have to implement size and clone as well as other methods that you wish to use. If you want to be really clean about this, you can even make the class implement List or implement Collection or so. This way you can still treat your class as a java.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 implement size, etc.

    public class Employees /*implements List<Employees>*/ {
        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 add(Employee w){
            employees.add(w);
        }
    
        public Employees clone() {
            return employees.clone();
        }
    
        // add more methods as you need them (like remove, get, size, etc)
    }
    

    2. Employees only extends LinkedList and doesn’t contain one
    Throw away your methods addWerknemer(Employee emp) and the copy constructor Employees(Employees) as well as your internal list. This way you will not overwrite the existing implementations of LinkedList. This approach is more or less useless because you basically just rename LinkedList to Employees and add/change nothing. Therefore I wouldn’t recommend this approach.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having problem in writing copy constructor for pointers to objects. This is
Having problems with a small awk script, Im trying to choose the newest of
Having problems iterating. Problem has to do with const correctness, I think. I assume
I'm having problems with trying to catch an error. I'm using Pyramid/SQLAlchemy and made
I am having erratic issue with my copy constructor. I have a class MyData
The page having problems is... http://schnell.dreamhosters.com/index.php?page=gallery# I use Firebug to debug my jQuery and
I've made a class template called Queue but I'm having problems trying to instanciate
I'm having problems copying a multidimensional vector, I've tried many things but this is
Having problems with the combining of two statements in a controller. Both statements work
Im having problems with displaying ONLY some elements ONLY on print page. For example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.