Simply put, I have a method with an ArrayList parameter. In the method I modify the contents of the ArrayList for purposes relevant only to what is returned by the method. Therefore, I do not want the ArrayList which is being passed as the parameter to be affected at all (i.e. not passed as a reference).
Everything I have tried has failed to achieve the desired effect. What do I need to do so that I can make use of a copy of the ArrayList within the method only, but not have it change the actual variable?
Yes, a List that you pass to a method is passed by reference. Any objects you add to the List inside the method will still be in the List after the method returns. Show activity on this post. If you add to a list in one method, its original reference in first method will also contain the new item.
Class one has an ArrayList as one of its attributes and it calls a void method from class two and passes that ArrayList as a parameter. Now that method initializes another ArrayList and makes it equal to the parameter passed by me and makes changes to that new ArrayList .
Everything in Java is passed by value.In case of an array (which is nothing but an Object), the array reference is passed by value (just like an object reference is passed by value). When you pass an array to other method, actually the reference to that array is copied.
The reason is that Java object variables are simply references that point to real objects in the memory heap. Therefore, even though Java passes parameters to methods by value, if the variable points to an object reference, the real object will also be changed.
If we want to pass an ArrayList as an argument to a function then we can easily do it using the syntax mentioned below. Example: ArrayList<Integer> list = new ArrayList<>(); modifyList(list); public static void modifyList(ArrayList<Integer> list){ list.add(69); list.add(98); }
In the code above when we passed the list to the callByValue function then the ‘parameterList’ starts pointing to the memory address i.e. 1000. But when we created a new instance of ArrayList and made ‘parameterList’ point to it then ‘parameterList’ starts pointing to a new memory address (let’s say) 2000.
This is happening because the Object References are passed by value in java as java is strictly Pass by Value . Implementation of the problem statement: How Java is pass by Value? Let us assume that the Array list object that we created inside the main function points to an address 1000.
Simply put, I have a method with an ArrayList parameter. In the method I modify the contents of the ArrayList for purposes relevant only to what is returned by the method. Therefore, I do not want the ArrayList which is being passed as the parameter to be affected at all (i.e. not passed as a reference).
Even if you had a way to pass the array list as a copy and not by reference it would have been only a shallow copy.
I would do something like:
void foo(final ArrayList list) { ArrayList listCopy = new ArrayList(list); // Rest of the code }
And just work on the copied list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With