Varargs - passing a list of arguments, of any size, to a method


So far, in our Java course, always specify the exact number of arguments that a method must receive.
When we will study Arrays and ArrayLists - soon , we should see that we can pass any number of values ​​to the methods.

But there is another way to do this in Java, using the ellipsis: ...
Using this, we can move one, two, three or any number of arguments to a method that it will know how to handle this data.


Varargs syntax:

In time to declare your method, use the ellipsis after the parameter type:

returnType methodName (ParameterType...parameterName) {
   // code of your method
}

Ready.
Java is smart enough to know that 'parameterName' is actually a list of values​​.

Example of use

Java Code: Create an application that receive from user 5 ​​values ​​and calculate the average of the firsts: 5, 4, 3 and 2 numbers; Use only one method that takes a list of arguments of any size.


import java.util.Scanner;

public class averages{
    
    public static float average(float... values){
        float average=0;
        
        for(float value: values){
            average += value;
        }
        
        return average/values.length;
    }

    public static void main(String[] args){
            float value1, value2, value3, value4, value5;
            Scanner input = new Scanner(System.in);
            
            System.out.print("Type number 1: ");
            value1 = input.nextFloat();
            System.out.print("Type number 2: ");
            value2 = input.nextFloat();
            System.out.print("Type number 3: ");
            value3 = input.nextFloat();
            System.out.print("Type number 4: ");
            value4 = input.nextFloat();
            System.out.print("Type number 5: ");
            value5 = input.nextFloat();
            
            
            System.out.println("The average of the 5 numbers is: " + average(value1,value2,value3,value4,value5));
            System.out.println("The average of the 4 firsts numbers is: " + average(value1,value2,value3,value4));
            System.out.println("The average of the 3 firsts numbers is " + average(value1,value2,value3));
            System.out.println("The average of the 2 firsts numbers is " + average(value1,value2));
        }


}



In this example, we use a special type of loop, used a lot in Arrays: the foreach loop.
But do not fear, it is quite easy to understand: it simply loops through all the elements of a list of variables.
In our case, our list of variables 'values'. Why list values​​?
However, because there is not only one value. We can send one, two, three ... thousand values​​.
Using this type of loop, it will go, always, ALL values ​​of this variable list.
At each iteration, each value of this list will be assigned to the variable 'value'.

The logic of the loop is to add each element of the list, ie adds 'value' to the variable 'average'.
At the end of the calculation, divides the value of this variable 'average' by the number of elements in the list.
This number is stored in a method called length and can be accessed via: average.length
This is a special property in this list of values​​. It now goes with a set of information. One such information is its number of elements.
You will learn more about these 'lists' values ​​when studying Arrays.
Wise that Java, does not?


Exercise:
A teacher, very cool, did 3 tests during a semester but will only take into account the two highest grades to average.
Write a Java application that asks the value of 3 notes, show how the average should be with the 3 tests, average with the 2 highest grades, as well as your highest score and your lowest note. These averages must be calculated using the same method, because you're a Java programmer and will not creat a lot of methods for nothing.
Create another method that receives all 3 grades and returns the largest. And another that returns the smallest.

Commented solution here

No comments: