3

I have this code. But I don't know how to explain the result:

ArrayList<String> first = new ArrayList<String>();
        first.add("1");
        first.add("2");
        first.add("3");

        ArrayList<String> second = new ArrayList<String>();
        second = first;
        System.out.println("before modified:"+second.size());
        second.clear();
        System.out.println("after modified:");
        System.out.println("   First:"+first.size());
        System.out.println("   Second:"+second.size());

The result will be: 3 / 0 /0

The problem I don't know is: when you assign first = second; so, both first and second array will point to same object (1,2 and 3). after you clear all elements on second array, so all reference between second array and these objects will loose (no problem here).

The thing I don't know is: but these objects (1,2 and 3) still hold reference to first array. Why first array's size is 0.

Please explain for me.

Thanks :)

Kai
  • 38,985
  • 14
  • 88
  • 103
hqt
  • 29,632
  • 51
  • 171
  • 250
  • See this link http://stackoverflow.com/questions/869033/how-do-i-copy-an-object-in-java – Amit Apr 12 '12 at 07:44

5 Answers5

6

By assigning second = first, there is only one arraylist with two references. The references are the same. So, when call clear using one of the two references (first or second), clear will be performed on the referenced arraylist.

This is something else than you first thought. It's not so that assigning the second = first all the references of the strings you added to the first one, will be copied into a new ArrayList object, that would be magic (in Java).

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
3

When you do first = second your ArrayList items will point to the same memory locations. Doing a .clear will remove the elements to which the ArrayList is pointing to. This will have repercussions on the other ArrayList.

If you just want to copy the elements of ArrayList1 to ArrayList2, you could do something like so: ArrayList<String> second = new ArrayList<String>(first);

npinti
  • 51,780
  • 5
  • 72
  • 96
1

but these objects (1,2 and 3) still hold reference to first array. Why first array's size is 0.

ArrayList<String> second = new ArrayList<String>();
second = first;

is the same as writing

ArrayList<String> second = first;

You have made second reference point to the first arraylist,it is not using a new arraylist. So when you call clear it clears the "first" arraylist created - you have two references pointing to one arraylist.

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
1

When you assign one ArrayList to two variable and modify any one of them, this will reflect in both.So operation performed in any of one variable also reflect in second one. (Single object referenced by two variable).

kundan bora
  • 3,821
  • 2
  • 20
  • 29
1

In Java a variable (except primitives) is always a reference (which has the start address of object) to an object only, Reference is never an object in itself.

For example

second = first;

is assigning a reference, so that first and second now refering to the same object. Objects are not copied, neither in assignments, nor in argument passing (what is copied/assigned is the reference).

Amit
  • 13,134
  • 17
  • 77
  • 148