How to Pass ArrayList Object as Function Argument in java?
ArrayList class in Java is basically a resizable array i.e. it can grow and shrink in size dynamically according to the values that we add or remove to/from it. 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.
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, we created an ArrayList object named ‘list’ and then we passed it to a function named modifyList. The changes that we did to this list (i.e. the values that we added to the list) will appear to the original list inside the main function. 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:
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
ArrayList<Integer> list = new ArrayList<>();
list.add( 3 );
list.add( 57 );
list.add( 7 );
System.out.print( "ArrayList after insertions: " );
display(list);
modifyList(list);
System.out.print( "ArrayList after modifications: " );
display(list);
}
public static void
modifyList(ArrayList<Integer> parameterList)
{
parameterList.add( 20 );
parameterList.add( 98 );
}
public static void display(ArrayList<Integer> list)
{
System.out.println(list);
}
}
|
Output
ArrayList after insertions: [3, 57, 7]
ArrayList after modifications: [3, 57, 7, 20, 98]
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. When we passed this object to the modifyList function then the address 1000 is passed to it and the object ‘parameterList’ also starts pointing to the same location inside the memory as that of ‘list’ that is why we said that object references are passed by value in java. After that when we did modifications to ‘parameterList’ then the same changes are made to the original ‘list’. This concept is very similar to what happens in C++ when we pass a pointer variable as an argument.
ArrayList<Integer> list = new ArrayList<>();
callByValue(list);
public static void callByValue(ArrayList<Integer> parameterList){
parameterList = new ArrayList<>();
parameterList.add(88);
parameterList.add(200);
parameterList.add(89);
}
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. The changes made to ArrayList inside this function will no longer affect the ArrayList at memory address 1000. Hence, the ‘list’ inside the main function remains unaffected.
Below is the implementation of the problem statement:
Java
import java.util.ArrayList;
public class GFG {
public static void main(String[] args)
{
ArrayList<Integer> list = new ArrayList<>();
list.add( 3 );
list.add( 57 );
list.add( 7 );
System.out.print( "ArrayList after insertions: " );
display(list);
callByValue(list);
System.out.print(
"ArrayList after the function call: " );
display(list);
}
public static void
callByValue(ArrayList<Integer> parameterList)
{
parameterList = new ArrayList<>();
parameterList.add( 88 );
parameterList.add( 200 );
parameterList.add( 89 );
System.out.print(
"New ArrayList inside the function: " );
display(parameterList);
}
public static void display(ArrayList<Integer> list)
{
System.out.println(list);
}
}
|
Output
ArrayList after insertions: [3, 57, 7]
New ArrayList inside the function: [88, 200, 89]
ArrayList after the function call: [3, 57, 7]
Last Updated :
29 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...