Given an Arrayl.ist that contains a list of Employee object references, describe two
ways in which you could write code to make a copy of that ArrayL ist. (Hint: one way
would be a shallow copy). Use code examples to illustrate your answer. Discuss the
implications of each way you have given above with regard to variable references
within the Arraylist and Employee objects.

Would this do?

public class Main {//SHALLOW COPY
  public static void main(String args[]) {
    ArrayList<String> al = new ArrayList<String>();

    al.add("C");
    al.add("A");
    al.add("E");
    al.add("java2s.com");
    al.add("D");
    al.add("F");
    al.add(1, "java2s.com");

    System.out.println(al);

    Object list =  al.clone();
    System.out.println(list);

  }

and how would a deep copy look like??
I would appreciate any help :-)

Recommended Answers

All 6 Replies

Deep copy means you need to recreate a new object for each item in the list before you add to the new list. In other words, create a new empty ArrayList, copy the object from the old list, and add the new copy object to the new list.

In your case, the String class is automatically copied if you use get() from the ArrayList because its class is immutable. However, be very careful that this is not always true for many others when the object is in a mutable class.

Like this?
ArrayList<Employee> aListCloned = (ArrayList<Employee>)aListOriginal.clone();

the String class is automatically copied if you use get() from the ArrayList

Have you any reference or demo for this? I would be very surprised if get did anything other than just return a ref to the original object.

Meybe this one is right?

public class JavaCloneArrayListExample {

    public static void main(String args[]){

        //create original ArrayList object
        ArrayList<Employee> aListOriginal = new ArrayList<Employee>();
        aListOriginal.add(new Employee("Mark", 10000));
        aListOriginal.add(new Employee("Steve", 20000));
        aListOriginal.add(new Employee("Alex", 30000));

        //to clone ArrayList use clone method
        ArrayList<Employee> aListCloned = (ArrayList<Employee>)aListOriginal.clone();


        //Change the object in original ArrayList
        aListCloned.get(0).setName("Name Updated");
        System.out.println("Original ArrayList " + aListOriginal);
        System.out.println("Cloned ArrayList " + aListCloned);

Have you any reference or demo for this? I would be very surprised if get did anything other than just return a ref to the original object.

You are correct again. Sorry for that assumption. Yes, it returns the object reference. The good thing is that the object is immutable so it would act as if it is a copy (its value is not allow to be changed).

OK. Yes, because Strings are immutable it never normally matters whether you have a ref to the origional or a copy. (Java uses this fact to perform all kinds of optimisation of Strings behind the scenes.) So for this particlar thread, a shallow copy and a deep copy of a Collection of Strings are indistinguishable The only way AFAIK to tell the difference is to use == on the String refs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.