{"id":"arrays-element-types","slug":"other-element-types","title":"Arrays of other types","text":"The elements can be of any type. The array's type is the element type followed by `[]`: `double[]`, `String[]`, `boolean[]`. Two arrays of the same length can work as a pair, where the same index in each describes the same thing:\n\n```java\nString[] dishes = new String[3];\ndouble[] prices = new double[3];\ndishes[0] = \"Soup\";\nprices[0] = 4.5;\ndishes[1] = \"Pasta\";\nprices[1] = 9.0;\ndishes[2] = \"Pie\";\nprices[2] = 3.75;\nfor (int i = 0; i < dishes.length; i++) {\n    System.out.println(dishes[i] + \": \" + prices[i]);\n}\n```\n\n```output\nSoup: 4.5\nPasta: 9.0\nPie: 3.75\n```\n\nAll the elements of one array have the same type, and a value of another type doesn't fit:\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        int[] ages = new int[2];\n        ages[0] = \"twelve\";\n    }\n}\n```\n\n```javac\nMain.java:4: error: incompatible types: String cannot be converted to int\n        ages[0] = \"twelve\";\n                  ^\n1 error\n```\n\nOne difference from lists: an array of whole numbers is written with the basic type, `int[]`. A list needs `ArrayList<Integer>`, but an array has no such rule.\n\n### Reading values into an array\n\nWhen the program is told how many values are coming, it can make an array of exactly that size. Here the first line of input says how many names follow:\n\n```java\nimport java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int count = Integer.valueOf(scanner.nextLine());\n        String[] guests = new String[count];\n        for (int i = 0; i < guests.length; i++) {\n            guests[i] = scanner.nextLine();\n        }\n        System.out.println(\"Guest list:\");\n        for (int i = 0; i < guests.length; i++) {\n            System.out.println((i + 1) + \". \" + guests[i]);\n        }\n    }\n}\n```\n\n```input\n3\nAmira\nBruno\nChen\n```\n\n```output\nGuest list:\n1. Amira\n2. Bruno\n3. Chen\n```\n\nThe size comes from the input, but once the array exists, it can't grow. When a program can't know the count before it starts reading (for example, when it reads until an empty line), a list is the better choice.\n\n### Swapping two elements\n\nTo swap two elements, keep one of them aside first. Otherwise its value is gone before it's copied:\n\n```java\nString[] podium = new String[3];\npodium[0] = \"Kim\";\npodium[1] = \"Lea\";\npodium[2] = \"Max\";\nString kept = podium[0];\npodium[0] = podium[1];\npodium[1] = kept;\nfor (String name : podium) {\n    System.out.println(name);\n}\n```\n\n```output\nLea\nKim\nMax\n```\n\nWithout `kept`, the line `podium[0] = podium[1];` would write over Kim, and both places would end up holding Lea.\n","task":"Fill in the blanks. The program reads how many prices follow, then the prices, and prints their total. For the input `3`, `2.5`, `4.0` and `1.25` it prints:\n\n```\nTotal: 7.75\n```\n","kind":"fill","seed":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int count = Integer.valueOf(scanner.nextLine());\n        [[double]][] prices = new double[count];\n        for (int i = 0; i < prices.length; i++) {\n            prices[i] = Double.valueOf(scanner.nextLine());\n        }\n        double total = 0;\n        for ([[double]] price : prices) {\n            total += price;\n        }\n        System.out.println(\"Total: \" + total);\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 count = Integer.valueOf(scanner.nextLine());\n        double[] prices = new double[count];\n        for (int i = 0; i < prices.length; i++) {\n            prices[i] = Double.valueOf(scanner.nextLine());\n        }\n        double total = 0;\n        for (double price : prices) {\n            total += price;\n        }\n        System.out.println(\"Total: \" + total);\n    }\n}\n","hints":["The array holds decimal numbers, so its type is an array of `double`.","The for-each variable has the element type too: both blanks are `double`."],"tests":[{"name":"Test 1","stdin":"3\n2.5\n4.0\n1.25\n","expect":"Total: 7.75","hidden":false},{"name":"Test 2","stdin":"2\n10\n0.5\n","expect":"Total: 10.5","hidden":true}],"require":[],"forbid":[],"more":[{"task":"A small theater numbers its seats from 0. Read the number of seats, then the numbers of the booked seats, one per line, until `-1`. Keep track of them in a `boolean[]` where `true` means booked. Then print every free seat, in order. For the input `5`, `1`, `3` and `-1` the program prints:\n\n```\nSeat 0 is free\nSeat 2 is free\nSeat 4 is free\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 seats = Integer.valueOf(scanner.nextLine());\n        boolean[] booked = new boolean[seats];\n        while (true) {\n            int seat = Integer.valueOf(scanner.nextLine());\n            if (seat == -1) {\n                break;\n            }\n            booked[seat] = true;\n        }\n        for (int i = 0; i < booked.length; i++) {\n            if (!booked[i]) {\n                System.out.println(\"Seat \" + i + \" is free\");\n            }\n        }\n    }\n}\n","hints":["A new `boolean[]` starts with every element `false`, so every seat starts free. Each booked number sets its element: `booked[seat] = true;`","After the reading loop, go through the indexes and print the ones where `!booked[i]`."],"tests":[{"name":"Test 1","stdin":"5\n1\n3\n-1\n","expect":"Seat 0 is free\nSeat 2 is free\nSeat 4 is free","hidden":false},{"name":"Test 2","stdin":"3\n0\n-1\n","expect":"Seat 1 is free\nSeat 2 is free","hidden":true},{"name":"Test 3","stdin":"4\n2\n2\n0\n-1\n","expect":"Seat 1 is free\nSeat 3 is free","hidden":true},{"name":"Test 4","stdin":"2\n-1\n","expect":"Seat 0 is free\nSeat 1 is free","hidden":true}],"require":[{"pattern":"new\\s+boolean\\s*\\[","message":"Keep track of the seats in a `boolean[]`."}],"forbid":[{"pattern":"\\bArrayList\\b","message":"Use an array for this one, not a list."}]},{"task":"The program reads a count and that many whole numbers into an array, and prints the array with a for-each loop. Add code in between that **reverses** the array: swap the first element with the last, the second with the second to last, and so on. For the input `5`, `1`, `2`, `3`, `4` and `5` the program prints:\n\n```\n5\n4\n3\n2\n1\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        int count = Integer.valueOf(scanner.nextLine());\n        int[] values = new int[count];\n        for (int i = 0; i < values.length; i++) {\n            values[i] = Integer.valueOf(scanner.nextLine());\n        }\n\n        // Reverse the array here.\n\n        for (int value : values) {\n            System.out.println(value);\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 count = Integer.valueOf(scanner.nextLine());\n        int[] values = new int[count];\n        for (int i = 0; i < values.length; i++) {\n            values[i] = Integer.valueOf(scanner.nextLine());\n        }\n\n        for (int i = 0; i < values.length / 2; i++) {\n            int other = values.length - 1 - i;\n            int kept = values[i];\n            values[i] = values[other];\n            values[other] = kept;\n        }\n\n        for (int value : values) {\n            System.out.println(value);\n        }\n    }\n}\n","hints":["Index `i` swaps with index `values.length - 1 - i`. Swap with a temporary variable, as in the podium example.","Go only halfway: `i < values.length / 2`. A loop over the whole array swaps every pair twice, which puts them all back."],"tests":[{"name":"Test 1","stdin":"5\n1\n2\n3\n4\n5\n","expect":"5\n4\n3\n2\n1","hidden":false},{"name":"Test 2","stdin":"4\n10\n20\n30\n40\n","expect":"40\n30\n20\n10","hidden":true},{"name":"Test 3","stdin":"1\n7\n","expect":"7","hidden":true},{"name":"Test 4","stdin":"2\n-1\n8\n","expect":"8\n-1","hidden":true}],"require":[{"pattern":"new\\s+int\\s*\\[","max":1,"message":"Use only the one array: reverse it by swapping its elements."},{"pattern":"for\\s*\\(\\s*int\\s+\\w+\\s*:\\s*values\\s*\\)","message":"Keep the for-each loop that prints the array: the array itself must be reversed."}],"forbid":[{"pattern":"\\bArrayList\\b","message":"Use an array for this one, not a list."}]}]}