Lists as parameters
A list can be a method's parameter, like any other value. The parameter's type is the list's whole type, such as ArrayList<String>:
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> guests = new ArrayList<>();
guests.add("Mira");
guests.add("Oskar");
printInvitations(guests);
ArrayList<Integer> scores = new ArrayList<>();
scores.add(34);
scores.add(52);
scores.add(41);
System.out.println("Best score: " + highest(scores));
}
public static void printInvitations(ArrayList<String> names) {
for (String name : names) {
System.out.println("Dear " + name + ", you're invited!");
}
}
public static int highest(ArrayList<Integer> numbers) {
int best = numbers.get(0);
for (int number : numbers) {
if (number > best) {
best = number;
}
}
return best;
}
}
Dear Mira, you're invited!
Dear Oskar, you're invited!
Best score: 52- The one
import java.util.ArrayList;at the top of the file covers every method in it. - The call gives the list by its name:
printInvitations(guests). As with any parameter, the names in the call and in the method don't have to match. - A method can return something worked out from the list: a sum, a count, the largest value. (
highestexpects at least one value: on an empty list,numbers.get(0)crashes.)
A method can also build a new list and return it. Its return type is then the list's type:
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> scores = new ArrayList<>();
scores.add(7);
scores.add(12);
scores.add(9);
System.out.println(withBonus(scores, 5));
System.out.println(scores);
}
public static ArrayList<Integer> withBonus(ArrayList<Integer> scores, int bonus) {
ArrayList<Integer> result = new ArrayList<>();
for (int score : scores) {
result.add(score + bonus);
}
return result;
}
}
[12, 17, 14]
[7, 12, 9]The original list is left as it was: the method only read it, and put the new values in a list of its own.
Challenges that pass a list
The checks in this step build a list, fill it, and call your method with it, just as main does above.