Sub topic 11
Arrays and Variable-Arity Methods
A method is a named set of instructions that can take input. An argument is a value you pass to a method when you call it.
Sometimes you want to pass a different number of values each time. For example, a method might add two scores in one call and five scores in another. A variable-arity parameter, often called a varargs parameter, makes this possible.
A parameter is a named input in a method’s definition. Write three dots after the type to make it a varargs parameter:
int... numbers
Inside the method, numbers is an int array. You can use its length, access its elements, or loop through it just like any other array.
A worked example
This program adds numbers passed to sum. It also shows what happens when a method changes an existing array.
Save it as VarargsDemo.java. Compile it with javac VarargsDemo.java, then run it with java VarargsDemo.
public class VarargsDemo {
static int sum(int... numbers) {
int total = 0;
for (int number : numbers) {
total += number;
}
return total;
}
static void addToFirst(int amount, int... numbers) {
if (numbers.length > 0) {
numbers[0] += amount;
}
}
public static void main(String[] args) {
System.out.println(sum());
System.out.println(sum(3, 4, 5));
int[] scores = {3, 4, 5};
System.out.println(sum(scores));
addToFirst(2, scores);
System.out.println(scores[0]);
}
}The program prints these values, one per line:
012125
Look at the calls to sum:
sum()passes no numbers. The method receives an empty array, so the loop does not run and the total stays at0.sum(3, 4, 5)passes three separate numbers. Java puts them in an array for the method.sum(scores)passes an existing array. The method loops through its elements in the same way.
The word return sends the total back to the code that called sum.
Mixing regular and varargs parameters
The addToFirst method has a regular parameter, amount, followed by a varargs parameter, numbers. In this call:
addToFirst(2, scores)
The value 2 goes into amount. The array scores goes into numbers. The method adds 2 to the first array element, changing it from 3 to 5.
The length check matters because a varargs array can be empty. Without the check, accessing its first element when it has no elements would cause an error.
A method can have only one varargs parameter, and it must be last. For example, int amount, int... numbers is valid. Putting another parameter after numbers is not.
Passing an existing array
When you pass an existing array to a varargs method, the method receives that same array, not a copy. Changes to its elements are visible to the caller. The example shows this when addToFirst changes scores[0].
The values for a varargs parameter must have the right type. For int... numbers, pass integer values or one int array. You cannot mix an array with extra individual numbers in the same varargs input, as in sum(scores, 2).
Also, you cannot define both sum(int... numbers) and sum(int[] numbers) in the same class. Java treats them as the same method for that purpose.
Exercise
What does sum(4, 6) return? What does sum() return?
Show answer
sum(4, 6) returns 10. sum() returns 0 because its array is empty.
Exercise
If scores is {3, 4, 5}, what is scores[0] after addToFirst(7, scores)?
Show answer
It is 10. The method changes the first element of the same array.