0

I'm getting weird problems in my android app and I think it may be linked to the behavior of the way the ArrayList works. Check the following code and please tell me if I'm correct or doing something wrong:

ArrayList<ArrayList<String>> arr = new ArrayList<ArrayList<String>>();
ArrayList<String> tmp = new ArrayList<String>();
tmp.add("test");
arr.add(tmp);
tmp.clear();

After the last line the contents of arr[0] is emptied. So does that mean that when adding one ArrayList to another it does it by reference?

So if I have the following method:

void addArray(ArrayList<String> arr) {
 group.add(arr); // group is ArrayList<ArrayList<String>>;
};

I must change it to:

void addArray(ArrayList<String> arr) {
 ArrayList<String> tmp = new ArrayList<String>();
 tmp.addAll(arr);
 group.add(tmp); // group is ArrayList<ArrayList<String>>;
};

to make sure that if I clear the incoming array somewhere else that nothing happens to the group array?

1
  • 2
    Yes, ArrayList (& all other collections) only stores references to objects, they do not clone or copy them. Commented Jan 24, 2012 at 9:40

3 Answers 3

3

In Java there is no passing by value, every object is passed by reference. So in your case arr[0] and tmp are the same object and clearing it will result in arr[0] being cleared. Hope this helps.

EDIT

As a quick answer to the second part of your question: you don't need to use the tmp ArrayList inside the addArray method. The argument of the addArray method is a reference to the object, passed by value. So changing it won't have any effect outside of the addArray method. Hope it's clear.

Sign up to request clarification or add additional context in comments.

8 Comments

It would be more precise to say that references are passed by value: javadude.com/articles/passbyvalue.htm :)
Thanks. I assume if I were to call "tmp = new ArrayList<String>" instead of "tmp.clear()" the same thing would happen?
@stevovo, no the value of the reference is copied. Creating a new ArrayList and changing tmp does not change what has been added to the ArrayList.
@Bombe I added some changes to my question. Is that the best way of making sure that my arrays don't lose changes if the arrays inside are cleared?
I thought that would be obvious by now: copy the elements of an ArrayList to a new ArrayList. The way you do it above in the question seems to be correct but I’m sure that by now you have simply tested your method and found that it does what you want.
|
0

Yes, objects are not cloned when they're added to a collection.

Comments

0

For objects Java always passes a copy of the reference value, so yes, the ArrayList you are clearing is the same as the one you added to the other ArrayList.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.