How to Pass ArrayList Object as Function Argument in java?
Last Updated :
29 Oct, 2022
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
// Java program to pass an
// ArrayList as an argument
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Creating an ArrayList Object of Integer type
ArrayList<Integer> list = new ArrayList<>();
// Inserting some Integer values in ArrayList
list.add(3);
list.add(57);
list.add(7);
// list after insertions : [3, 57, 7]
// Displaying the ArrayList
System.out.print("ArrayList after insertions: ");
display(list);
// Passing the ArrayList as an argument for
// modification The modifications done to the list
// inside the function will appear inside the
// original ArrayList inside main()
modifyList(list);
// list after modifications : [3, 57, 7, 20, 98]
// displaying the ArrayList after modifications
System.out.print("ArrayList after modifications: ");
display(list);
}
// Function to modify the arrayList
public static void
modifyList(ArrayList<Integer> parameterList)
{
parameterList.add(20);
parameterList.add(98);
}
// Function to display the array List
public static void display(ArrayList<Integer> list)
{
System.out.println(list);
}
}
OutputArrayList 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
// Java program to pass an
// ArrayList as an argument
import java.util.ArrayList;
public class GFG {
public static void main(String[] args)
{
// Creating an ArrayList Object of Integer type
ArrayList<Integer> list = new ArrayList<>();
// Inserting some Integer values in ArrayList
list.add(3);
list.add(57);
list.add(7);
// list after insertions : [3, 57, 7]
// Displaying the ArrayList
System.out.print("ArrayList after insertions: ");
display(list);
// The changes made to the ArrayList inside this
// function will not affect the original ArrayList
// that we pass to it as we are simply creating the
// new instance of ArrayList and making our
// parameterList point to it
callByValue(list);
// list after function call : [3, 57, 7]
// displaying the ArrayList after calling
// callByValue
System.out.print(
"ArrayList after the function call: ");
display(list);
}
public static void
callByValue(ArrayList<Integer> parameterList)
{
// Creating a new instance of ArrayList
parameterList = new ArrayList<>();
// Inserting Integer values to the new ArrayList
// created inside the function
parameterList.add(88);
parameterList.add(200);
parameterList.add(89);
// Displaying our new ArrayList
System.out.print(
"New ArrayList inside the function: ");
display(parameterList);
}
// Function to display the array List
public static void display(ArrayList<Integer> list)
{
System.out.println(list);
}
}
OutputArrayList after insertions: [3, 57, 7]
New ArrayList inside the function: [88, 200, 89]
ArrayList after the function call: [3, 57, 7]
Similar Reads
How Objects Can an ArrayList Hold in Java? ArrayList is a part of the collection framework and is present in java.util package. It provides us with dynamic arrays in Java just as Vector in C++. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. In order to understan
3 min read
How to clone an ArrayList to another ArrayList in Java? The clone() method of the ArrayList class is used to clone an ArrayList to another ArrayList in Java as it returns a shallow copy of its caller ArrayList. Syntax: public Object clone(); Return Value: This function returns a copy of the instance of Object. Below program illustrate the Java.util.Arra
2 min read
How to Convert an ArrayList of Objects to a JSON Array in Java? In Java, an ArrayList is a resizable array implementation of the List interface. It implements the List interface and is the most commonly used implementation of List. In this article, we will learn how to convert an ArrayList of objects to a JSON array in Java. Steps to Convert an ArrayList of Obje
3 min read
How to Declare an ArrayList with Values in Java? ArrayList is simply known as a resizable array. Declaring an ArrayList with values is a basic task that involves the initialization of a list with specific elements. It is said to be dynamic as the size of it can be changed. Proceeding with the declaration of ArrayList, we have to be aware of the co
2 min read
How to Convert an ArrayList into a JSON String in Java? In Java, an ArrayList is a resizable array implementation of the List interface. It implements the List interface and is the most commonly used implementation of List. In this article, we will learn how to convert an ArrayList into a JSON string in Java. Steps to convert an ArrayList into a JSON str
2 min read
How to Add Element in Java ArrayList? Java ArrayList class uses a dynamic array for storing the elements. It is like an array, but there is no size limit. We can add or remove elements anytime. So, it is much more flexible than the traditional array. Element can be added in Java ArrayList using add() method of java.util.ArrayList class.
2 min read
How to Swap Two Elements in an ArrayList in Java? We can swap two elements of Array List using Collections.swap() method. This method accepts three arguments. The first argument is the ArrayList and the other two arguments are the indices of the elements. This method returns nothing. Syntax: public static void swap(List list, int a, int b); Parame
2 min read
Java Program to Empty an ArrayList in Java ArrayList class is a resizable array, present in 'java.util package'. The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (i.e. if you want to append/add or remove element(s) to/from an array, you have to create a new array. However, elem
3 min read
How to Serialize ArrayList in Java? ArrayList is a class under the collections framework of java. It is present in java.util package. An ArrayList is a re-sizable array in java i.e., unlike an array, the size of an ArrayList can be modified dynamically according to our requirement. Also, the ArrayList class provides many useful method
2 min read
Convert ArrayList to Vector in Java There are several ways to convert ArrayList to Vector. We can use a vector constructor for converting ArrayList to vector. We can read ArrayList elements one by one and add them in vector. Approach 1: (Using Vector Constructor) Create an ArrayList.Add elements in ArrayList.Create a vector and pass t
3 min read