Arrays and methods
From here on, the examples create their arrays the short way: int[] scores = {4, 9, 6}; makes an array of three ints that holds 4, 9 and 6. The last step of the module looks at it more closely.
A method can take an array as a parameter. The parameter's type is the array type, such as int[] values:
public class Main {
public static void main(String[] args) {
int[] scores = {4, 9, 6};
System.out.println("Total: " + total(scores));
addBonus(scores, 10);
for (int score : scores) {
System.out.println(score);
}
}
public static int total(int[] values) {
int sum = 0;
for (int value : values) {
sum += value;
}
return sum;
}
public static void addBonus(int[] values, int bonus) {
for (int i = 0; i < values.length; i++) {
values[i] += bonus;
}
}
}
Total: 19
14
19
16addBonus changed main's array. As with lists in module 14, an array variable doesn't hold the elements themselves but a reference to the array. The method gets a copy of that reference, and the copy leads to the same array, so the change shows in main too. (Module 10's rule still holds: the parameter is a copy, but what's copied is the reference.)
Assignment works the same way as with lists. int[] other = scores; doesn't copy the array: it gives the same array a second name, and other[0] = 1; changes scores[0] too.
Returning a new array
A method can also create an array and return it. The return type is the array type:
public class Main {
public static void main(String[] args) {
int[] prices = {10, 25, 40};
int[] sale = halved(prices);
for (int i = 0; i < prices.length; i++) {
System.out.println(prices[i] + " on sale: " + sale[i]);
}
}
public static int[] halved(int[] values) {
int[] result = new int[values.length];
for (int i = 0; i < values.length; i++) {
result[i] = values[i] / 2;
}
return result;
}
}
10 on sale: 5
25 on sale: 12
40 on sale: 20halved makes a new array of the same length with new, fills it, and returns it. The original prices stay as they were. So a method that changes the array it's given is the right tool when the caller wants the change, and one that returns a new array is right when the original must be kept.
The checks in this step create an array and pass it to your method, for example int[] values = {3, 8, 5, 10}; and then System.out.println(countAbove(values, 4));.