Java Arena
Step 5 of 6

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;
        }
    }
}
Output
Total: 19
14
19
16

addBonus 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;
    }
}
Output
10 on sale: 5
25 on sale: 12
40 on sale: 20

halved 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));.

Your task

Challenge 1 of 3

Fill in the blanks of countAbove, which returns how many elements of the array are greater than limit. The check runs int[] values = {3, 8, 5, 10}; and then System.out.println(countAbove(values, 4));, and expects:

3

Challenge 2

What does this program print? The method gets an array and an int, and changes both of its parameters.

Challenge 3

Write a method runningTotals(int[] values) that returns a new array of the same length, where each element is the sum of the values up to and including that index. The array it's given must stay as it was. The check passes the array {3, 1, 4, 2} and prints the elements of the array your method returns, one per line:

3
4
8
10