Example 1: Pass ArrayList as Function Parameter
import java.util.ArrayList;
class Main {
public static void display(ArrayList<String> languages) {
System.out.print("ArrayList: ");
for(String language : languages) {
System.out.print(language + ", ");
}
}
public static void main(String[] args) {
// create an arraylist
ArrayList<String> languages = new ArrayList<>();
languages.add("Java");
languages.add("Python");
languages.add("JavaScript");
// passing arraylist as function parameter
display(languages);
}
}
Output
ArrayList: Java, Python, JavaScript,
In the above example, we have created an arraylist named languages. Here, we have a method display()
. It prints elements of arraylist.
Notice the line,
display(languages);
Here, we have passed languages as the function parameter.
Example 2: Pass ArrayList as Function Parameter by converting into Array
import java.util.ArrayList;
class Main {
public static void percentage(Integer[] marks) {
int totalMarks = 300;
int obtainedMarks = 0;
for(int mark : marks) {
obtainedMarks += mark;
}
// compute average
double percent = (obtainedMarks * 100) / totalMarks;
System.out.println("Percentage: " + percent);
}
public static void main(String[] args) {
// create an arraylist
ArrayList<Integer> marks = new ArrayList<>();
marks.add(67);
marks.add(87);
marks.add(56);
System.out.println("Marks: " + marks);
// passing arraylist as function parameter
percentage(marks.toArray(new Integer[marks.size()]));
}
}
Output
Marks: [67, 87, 56] Percentage: 70.0
In the above example, we have created an arraylist named marks. Notice the line,
percentage(marks.toArray(new Integer[0]));
Here, we are passing the arraylist as an argument to the percentage()
method.