Java method parameter list object
Ngày đăng:
14/12/2021
Trả lời:
0
Lượt xem:
52
Java Program to Pass ArrayList as the function argumentIn this example, we will learn to pass an arraylist as the funcion argument in Java. Show To understand this example, you should have the knowledge of the following Java programming topics:
Example 1: Pass ArrayList as Function Parameterimport java.util.ArrayList; class Main { public static void display(ArrayListOutput 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 Arrayimport 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 ArrayListOutput Marks: [67, 87, 56] Percentage: 70.0In 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. |