Passing an ArrayList as a parameter

Let me start by saying that I can’t enter the code here because the Internet doesn’t work on my laptop, so I send it through my phone. Okay, the problem is that I have two classes: first and second classes. The first class has an ArrayList as one of its attributes and calls the void method from the second class and passes ArrayList as a parameter. Now this method initializes another ArrayList and makes it equal to the parameter passed by me and makes changes to this new ArrayList . It's funny that even my original ArrayList, which was passed as a parameter, is also changing. What could be the possible reason?

+6
source share
6 answers

The problem is that when you use = to make a new ArrayList a copy of the original, you simply create a new link to the same ArrayList. Think of it as two variables pointing to the same object.

Check this out, it can help you understand what is going on: Is Java "pass-by-reference" or "pass by value",

To solve your problem, you need to create a new ArrayList using the "new" keyword, and then add all the objects or use the clone () method.

+5
source

The reason is that when passing an ArrayList as an argument, the called method can modify the contents of the array. ArrayList contains references to objects. If you want to avoid some class changing the contents of your ArrayList, you have to return a copy of your ArrayList, where all the objects inside are clones of the () s objects in your list.

Use object.clone ()

 ArrayList clonedCopy = new ArrayList(list1.size()); for (Object obj : list1) { clonedCopy.add(obj.clone()); } 

Now return this cloned Mine. But make sure obj is cloned!

+3
source

Because they point to the same link.

+1
source

Code example:

 public class MethodArguments { public static void main(String args[]) { ArrayList<String> a = new ArrayList<String>(); a.add("Steve"); a.add("Daniel"); a.add("John"); a.add("Maxi"); a.add("Jeni"); System.out.println(a); display(a); getSize(a); } static void display(ArrayList<String> arrayList1) { arrayList1.add("Pollard"); System.out.println(arrayList1); // passing the arraylist values and // adding the element } static void getSize(ArrayList<String> arrayList1) { System.out.println(arrayList1.size()); // getting the size of arraylist // by passing arguments to // method } } 

Conclusion:

[Steve, Daniel, John, Maxi, Janey]

[Steve, Daniel, John, Maxi, Janey, Pollard]

6

+1
source

The = operator in Java simply copies the ArrayList reference (the same for all objects). See this answer for creating a deep copy of ArrayList.

0
source

This is because the list of new arrays points to the same old array when you make it equal.

This small example should clarify this.

 import java.util.ArrayList; import java.util.List; public class JavaApplication1 { public static void main(String[] args) { List <String> origList = new ArrayList<>(); origList.add("a"); origList.add("b"); JavaApplication1 app = new JavaApplication1(); app.addToList(origList); for(String str:origList){ System.out.println(str); } } private void addToList(List<String> strList){ System.out.println("inside addToList"); List <String> newList = new ArrayList<>(); // newList = strList; //This is how you are doing it newList.addAll(strList); //This is how you should do it. newList.add("x"); } } 
0
source

All Articles