{"id":"arrays-in-methods","slug":"arrays-and-methods","title":"Arrays and methods","text":"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.\n\nA method can take an array as a parameter. The parameter's type is the array type, such as `int[] values`:\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        int[] scores = {4, 9, 6};\n        System.out.println(\"Total: \" + total(scores));\n        addBonus(scores, 10);\n        for (int score : scores) {\n            System.out.println(score);\n        }\n    }\n\n    public static int total(int[] values) {\n        int sum = 0;\n        for (int value : values) {\n            sum += value;\n        }\n        return sum;\n    }\n\n    public static void addBonus(int[] values, int bonus) {\n        for (int i = 0; i < values.length; i++) {\n            values[i] += bonus;\n        }\n    }\n}\n```\n\n```output\nTotal: 19\n14\n19\n16\n```\n\n`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.)\n\nAssignment 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.\n\n### Returning a new array\n\nA method can also create an array and return it. The return type is the array type:\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        int[] prices = {10, 25, 40};\n        int[] sale = halved(prices);\n        for (int i = 0; i < prices.length; i++) {\n            System.out.println(prices[i] + \" on sale: \" + sale[i]);\n        }\n    }\n\n    public static int[] halved(int[] values) {\n        int[] result = new int[values.length];\n        for (int i = 0; i < values.length; i++) {\n            result[i] = values[i] / 2;\n        }\n        return result;\n    }\n}\n```\n\n```output\n10 on sale: 5\n25 on sale: 12\n40 on sale: 20\n```\n\n`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.\n\nThe 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));`.\n","task":"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:\n\n```\n3\n```\n","kind":"fill","seed":"public class Main {\n    public static void main(String[] args) {\n        int[] ages = {12, 35, 8, 41, 19};\n        System.out.println(countAbove(ages, 18));\n    }\n\n    public static [[int]] countAbove([[int[] values]], int limit) {\n        int count = 0;\n        for (int value : values) {\n            if (value > limit) {\n                count++;\n            }\n        }\n        return count;\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        int[] ages = {12, 35, 8, 41, 19};\n        System.out.println(countAbove(ages, 18));\n    }\n\n    public static int countAbove(int[] values, int limit) {\n        int count = 0;\n        for (int value : values) {\n            if (value > limit) {\n                count++;\n            }\n        }\n        return count;\n    }\n}\n","hints":["The method returns a count, which is a whole number.","A parameter is a type and a name. The type here is an array of ints, and the loop calls it `values`: `int[] values`."],"tests":[{"name":"Test 1","stdin":"","call":"int[] values = {3, 8, 5, 10};\nSystem.out.println(countAbove(values, 4));\n","expect":"3","hidden":false},{"name":"Test 2","stdin":"","call":"int[] values = {1, 2};\nSystem.out.println(countAbove(values, 5));\n","expect":"0","hidden":true},{"name":"Test 3","stdin":"","call":"int[] values = {7, 6, 7};\nSystem.out.println(countAbove(values, 6));\n","expect":"2","hidden":true}],"require":[],"forbid":[],"more":[{"task":"What does this program print? The method gets an array and an int, and changes both of its parameters.\n","kind":"predict","seed":"public class Main {\n    public static void main(String[] args) {\n        int[] counts = {1, 2, 3};\n        int single = 1;\n        grow(counts, single);\n        System.out.println(counts[0] + \" \" + counts[1] + \" \" + counts[2]);\n        System.out.println(single);\n        int[] same = counts;\n        same[2] = 0;\n        System.out.println(counts[2]);\n    }\n\n    public static void grow(int[] values, int number) {\n        values[0] = 10;\n        number = 10;\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        int[] counts = {1, 2, 3};\n        int single = 1;\n        grow(counts, single);\n        System.out.println(counts[0] + \" \" + counts[1] + \" \" + counts[2]);\n        System.out.println(single);\n        int[] same = counts;\n        same[2] = 0;\n        System.out.println(counts[2]);\n    }\n\n    public static void grow(int[] values, int number) {\n        values[0] = 10;\n        number = 10;\n    }\n}\n","hints":["`values` leads to `main`'s own array, so `values[0] = 10` changes it. `number` is only a copy of `single`, so `single` stays 1.","`int[] same = counts;` doesn't make a new array: `same[2] = 0` changes `counts[2]`."],"tests":[{"name":"Output","stdin":"","expect":"10 2 3\n1\n0","hidden":false}],"require":[],"forbid":[],"lines":["10 2 3","1","0"]},{"task":"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:\n\n```\n3\n4\n8\n10\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        int[] values = {3, 1, 4, 2};\n        int[] totals = runningTotals(values);\n        for (int total : totals) {\n            System.out.println(total);\n        }\n    }\n\n    // Write the runningTotals method here.\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        int[] values = {3, 1, 4, 2};\n        int[] totals = runningTotals(values);\n        for (int total : totals) {\n            System.out.println(total);\n        }\n    }\n\n    public static int[] runningTotals(int[] values) {\n        int[] totals = new int[values.length];\n        int sum = 0;\n        for (int i = 0; i < values.length; i++) {\n            sum += values[i];\n            totals[i] = sum;\n        }\n        return totals;\n    }\n}\n","hints":["The method starts `public static int[] runningTotals(int[] values) {` and creates `new int[values.length]`.","Keep a sum in a variable. In each round, add `values[i]` to it and store the sum at `totals[i]`. Return `totals` at the end."],"tests":[{"name":"Test 1","stdin":"","call":"int[] values = {3, 1, 4, 2};\nint[] totals = runningTotals(values);\nfor (int total : totals) {\n    System.out.println(total);\n}\n","expect":"3\n4\n8\n10","hidden":false},{"name":"Test 2","stdin":"","call":"int[] values = {5};\nint[] totals = runningTotals(values);\nSystem.out.println(totals.length);\nSystem.out.println(totals[0]);\n","expect":"1\n5","hidden":true},{"name":"Test 3","stdin":"","call":"int[] values = {2, 2, 2};\nint[] totals = runningTotals(values);\nSystem.out.println(totals[0] + \" \" + totals[1] + \" \" + totals[2]);\nSystem.out.println(values[0] + \" \" + values[1] + \" \" + values[2]);\n","expect":"2 4 6\n2 2 2","hidden":true},{"name":"Test 4","stdin":"","call":"int[] values = {10, -4, 0, 7, -20};\nint[] totals = runningTotals(values);\nfor (int total : totals) {\n    System.out.println(total);\n}\n","expect":"10\n6\n6\n13\n-7","hidden":true}],"require":[],"forbid":[]}]}