Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing ArrayList as value only and not reference

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?

like image 366
fvgs Avatar asked Apr 03 '13 04:04

fvgs


People also ask

Is ArrayList passed by value or reference?

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.

How do you pass an ArrayList as an argument?

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 .

Are arrays pass by value or reference in Java?

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.

Why Java is pass by value and not pass by reference?

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.

How to pass an ArrayList as an argument to a function?

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); }

What is the difference between ‘parameterlist’ and ArrayList?

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.

Is Java pass by value or pass by reference?

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.

Can I modify the contents of an ArrayList in a method?

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).


1 Answers

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.

like image 89
Avi Avatar answered Sep 29 '22 05:09

Avi