In this article, we will understand how to pass ArrayList as the function argument. The ArrayList class is a resizable array, which can be found in the 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.
Below is a demonstration of the same −
Suppose our input is −
Run the program
The desired output would be −
The list is defined as: Java Python Scala Mysql Redshift
Algorithm
Step 1 - START Step 2 - Declare namely Step 3 - Define the values. Step 4 - Create an ArrayList, and iterate over it, and display it. Step 5 - In the main method, create the ArrayList, and add elements to it using the ‘add’ method. Step 6 - Display this on the console. Step 7 - Stop
Example 1
Here, we iterate a string array list.
import java.util.ArrayList;
public class Demo {
public static void print(ArrayList<String> input_list) {
System.out.print("\nThe list is defined as:\n ");
for(String language : input_list) {
System.out.print(language + " ");
}
}
public static void main(String[] args) {
System.out.println("The required packages have been imported");
ArrayList<String> input_list = new ArrayList<>();
input_list.add("Java");
input_list.add("Python");
input_list.add("Scala");
input_list.add("Mysql");
input_list.add("Redshift");
print(input_list);
}
}Output
The required packages have been imported The list is defined as: Java Python Scala Mysql Redshift
Example 2
Here, we iterate an integer array list.
import java.util.ArrayList;
public class Demo {
public static void print(ArrayList<String> input_list) {
System.out.print("\nThe list is defined as:\n ");
for(String language : input_list) {
System.out.print(language + " ");
}
}
public static void main(String[] args) {
System.out.println("The required packages have been imported");
ArrayList<String> input_list = new ArrayList<>();
input_list.add("Java");
input_list.add("Python");
input_list.add("Scala");
input_list.add("Mysql");
input_list.add("Redshift");
print(input_list);
}
}Output
The required packages have been imported The list is defined as: Java Python Scala Mysql Redshift