Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing ArrayList as a parameter

Let me start by saying that I can't put any code here because Internet on my laptop is not working so I am posting this through my phone. Okay the problem is that say I have two classes: class one and two. 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. Funny thing is that even my original ArrayList which was passed as parameter is also changing. What could be the possible reason?

like image 393
Sahil Chaudhary Avatar asked Nov 22 '12 00:11

Sahil Chaudhary


People also ask

Can an ArrayList be a parameter?

util. But ArrayList is a parameterized type. A parameterized type can take a type parameter, so that from the single class ArrayList, we get a multitude of types including ArrayList<String>, ArrayList<Color>, and in fact ArrayList<T> for any object type T.

Can we pass List as parameter in Java?

It is present in java. util package. If we want to pass an ArrayList as an argument to a function then we can easily do it using the syntax mentioned below. In the code above, we created an ArrayList object named 'list' and then we passed it to a function named modifyList.

Is ArrayList pass by reference in Java?

Yes, because You just pass a reference of ArrayList -Object, to your Object .


2 Answers

Sample Code:

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
 }

}

Output:

[Steve, Daniel, John, Maxi, Jeni]

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

6

like image 109
Steve Avatar answered Sep 30 '22 14:09

Steve


The problem is that when you use = to make the new ArrayList a copy of the original, you're just creating a new reference to the same ArrayList. Think of it as two variables pointing at the same object.

Check this out, it might help you understand what's happening: Is Java "pass-by-reference" or "pass-by-value"?

In order to solve your problem, you need to create a new ArrayList by using the "new" keyword and then adding all of the objects, or use the clone() method.

like image 44
mauro.dec Avatar answered Sep 30 '22 14:09

mauro.dec