{"id":"arrays-creating","slug":"creating-an-array","title":"Creating an array","text":"A list grows as you add to it. An **array** is a simpler collection: you say how big it is when you create it, and that size never changes. In return, every place in it exists from the start, ready to use.\n\n```java\nint[] numbers = new int[5];\nnumbers[2] = 7;\nnumbers[0] = 3;\nSystem.out.println(numbers[0]);\nSystem.out.println(numbers[2]);\nSystem.out.println(numbers[4]);\n```\n\n```output\n3\n7\n0\n```\n\n- `int[]` is the type \"array of ints\". The square brackets make it an array.\n- `new int[5]` creates an array with room for five ints. Their indexes are 0 to 4, as in a list.\n- `numbers[2] = 7;` stores 7 at index 2. There's no `add`: the place is already there, and you assign to it.\n- `numbers[2]` reads the value at index 2, where a list would say `list.get(2)`.\n\nIndex 4 was never given a value, and it holds 0. The array has exactly five places for as long as it exists: there's no way to make it hold a sixth value later.\n\n### Default values\n\nA new array isn't empty: every element starts with a default value. It's 0 for `int`, 0.0 for `double`, `false` for `boolean`, and `null` for `String`. (`null` means \"no value here yet\".)\n\n```java\ndouble[] prices = new double[3];\nboolean[] done = new boolean[3];\nString[] names = new String[3];\nSystem.out.println(prices[0]);\nSystem.out.println(done[1]);\nSystem.out.println(names[2]);\n```\n\n```output\n0.0\nfalse\nnull\n```\n\n### An element works like a variable\n\nEach element behaves like a variable of the element type. You can use its value in a calculation, and change it with `=`, `+=` or `++`. The index can be any whole-number expression, such as a variable:\n\n```java\nint[] goals = new int[3];\ngoals[1] = 2;\ngoals[1]++;\ngoals[0] = goals[1] * 2;\nint last = 2;\ngoals[last] += 5;\nSystem.out.println(goals[0] + \" \" + goals[1] + \" \" + goals[2]);\n```\n\n```output\n6 3 5\n```\n","task":"A parking garage has four levels, numbered 0 to 3. Fill in the blanks: make the array big enough for the four levels, and give level 3 its 20 free spots. Level 2 is never assigned, so it keeps the default value. The program prints:\n\n```\nLevel 0: 12 free\nLevel 1: 5 free\nLevel 2: 0 free\nLevel 3: 20 free\n```\n","kind":"fill","seed":"public class Main {\n    public static void main(String[] args) {\n        int[] freeSpots = new int[[[4]]];\n        freeSpots[0] = 12;\n        freeSpots[1] = 5;\n        freeSpots[[[3]]] = 20;\n        System.out.println(\"Level 0: \" + freeSpots[0] + \" free\");\n        System.out.println(\"Level 1: \" + freeSpots[1] + \" free\");\n        System.out.println(\"Level 2: \" + freeSpots[2] + \" free\");\n        System.out.println(\"Level 3: \" + freeSpots[3] + \" free\");\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        int[] freeSpots = new int[4];\n        freeSpots[0] = 12;\n        freeSpots[1] = 5;\n        freeSpots[3] = 20;\n        System.out.println(\"Level 0: \" + freeSpots[0] + \" free\");\n        System.out.println(\"Level 1: \" + freeSpots[1] + \" free\");\n        System.out.println(\"Level 2: \" + freeSpots[2] + \" free\");\n        System.out.println(\"Level 3: \" + freeSpots[3] + \" free\");\n    }\n}\n","hints":["The number in `new int[...]` is how many elements the array has: one for each level.","The levels are 0, 1, 2 and 3, so the size is 4, and the second blank is the index 3."],"tests":[{"name":"Output","stdin":"","expect":"Level 0: 12 free\nLevel 1: 5 free\nLevel 2: 0 free\nLevel 3: 20 free","hidden":false}],"require":[],"forbid":[],"more":[{"task":"Read three whole numbers into an array of three ints: the first to index 0, the second to index 1 and the third to index 2. Then print the elements in the reverse order, from index 2 down to index 0. For the input `4`, `9` and `1` the program prints:\n\n```\n1\n9\n4\n```\n","kind":"code","seed":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n\n    }\n}\n","solution":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int[] numbers = new int[3];\n        numbers[0] = Integer.valueOf(scanner.nextLine());\n        numbers[1] = Integer.valueOf(scanner.nextLine());\n        numbers[2] = Integer.valueOf(scanner.nextLine());\n        System.out.println(numbers[2]);\n        System.out.println(numbers[1]);\n        System.out.println(numbers[0]);\n    }\n}\n","hints":["Create the array first: `int[] numbers = new int[3];`","Each number goes straight into its place: `numbers[0] = Integer.valueOf(scanner.nextLine());`, and so on. Then print `numbers[2]`, `numbers[1]` and `numbers[0]`."],"tests":[{"name":"Test 1","stdin":"4\n9\n1\n","expect":"1\n9\n4","hidden":false},{"name":"Test 2","stdin":"10\n20\n30\n","expect":"30\n20\n10","hidden":true},{"name":"Test 3","stdin":"-5\n0\n-5\n","expect":"-5\n0\n-5","hidden":true}],"require":[{"pattern":"new\\s+int\\s*\\[\\s*3\\s*\\]","message":"Keep the numbers in an array made with `new int[3]`."}],"forbid":[{"pattern":"\\bArrayList\\b","message":"Use an array for this one, not a list."}]},{"task":"What does this program print? Keep track of each element, and remember the default value.\n","kind":"predict","seed":"public class Main {\n    public static void main(String[] args) {\n        int[] points = new int[4];\n        points[1] = 5;\n        points[3] = points[1] * 2;\n        points[1]++;\n        points[0] = points[3] - points[1];\n        System.out.println(points[0]);\n        System.out.println(points[1]);\n        System.out.println(points[2]);\n        System.out.println(points[3]);\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        int[] points = new int[4];\n        points[1] = 5;\n        points[3] = points[1] * 2;\n        points[1]++;\n        points[0] = points[3] - points[1];\n        System.out.println(points[0]);\n        System.out.println(points[1]);\n        System.out.println(points[2]);\n        System.out.println(points[3]);\n    }\n}\n","hints":["`points[3]` is set while `points[1]` is still 5, so it becomes 10. Only after that does `points[1]` go up to 6.","`points[2]` is never assigned, so it still holds the default 0."],"tests":[{"name":"Output","stdin":"","expect":"4\n6\n0\n10","hidden":false}],"require":[],"forbid":[],"lines":["4","6","0","10"]}]}