{"id":"multidimensional-arrays-grid","slug":"a-grid-of-values","title":"A grid of values","text":"An array keeps its values in a line, and one index says where a value is. Some data goes in two directions. A cinema's seats are in rows, and each row has seats from left to right. A spreadsheet has rows and columns, and so does a picture made of pixels. For data like that, Java has **two-dimensional arrays**, where each value has two indexes: its row and its column.\n\nHere the values are the brightness of the pixels of a tiny picture, from 0 for black to 9 for white:\n\n```java\nint[][] image = new int[3][4];\nimage[1][2] = 9;\nimage[0][3] = 4;\nSystem.out.println(image[1][2]);\nSystem.out.println(image[0][3]);\nSystem.out.println(image[2][0]);\n```\n\n```output\n9\n4\n0\n```\n\n- `int[][]` is the type \"two-dimensional array of ints\". Two pairs of brackets, two indexes.\n- `new int[3][4]` creates a grid of 3 rows with 4 columns each: 12 ints, all 0 to start with.\n- `image[1][2] = 9;` stores 9 in row 1, column 2. The row comes first, then the column.\n- `image[1][2]` reads the value back. Both indexes start from 0, as always.\n\nThis is the grid after the two assignments:\n\n| | column 0 | column 1 | column 2 | column 3 |\n|---|---|---|---|---|\n| **row 0** | 0 | 0 | 0 | 4 |\n| **row 1** | 0 | 0 | 9 | 0 |\n| **row 2** | 0 | 0 | 0 | 0 |\n\nThe order of the indexes matters: `image[2][1]` is row 2, column 1, a different cell from `image[1][2]`. Here it still holds 0.\n\n### A cell works like a variable\n\nEach cell behaves like a variable of the element type, just as the elements of an array do. You can use its value in a calculation and change it with `=`, `+=`, `++` or `--`, and the indexes can be any whole-number expressions:\n\n```java\nint[][] image = new int[3][4];\nimage[1][2] = 9;\nimage[1][2]--;\nint row = 2;\nimage[row][row + 1] = image[1][2] / 2;\nSystem.out.println(image[1][2] + \" \" + image[2][3]);\n```\n\n```output\n8 4\n```\n\n### Default values\n\nA new grid starts with the same default values as a new array: 0 for `int`, 0.0 for `double`, `false` for `boolean` and `null` for `String`. That suits a cinema's seat map. In a `boolean[][]` where `true` means booked, every seat starts free:\n\n```java\nboolean[][] booked = new boolean[5][8];\nbooked[2][3] = true;\nbooked[2][4] = true;\nSystem.out.println(booked[2][3]);\nSystem.out.println(booked[2][5]);\nSystem.out.println(booked[0][0]);\n```\n\n```output\ntrue\nfalse\nfalse\n```\n\nPeople count rows and seats from 1, but the indexes start from 0. The ticket for row 3, seat 4 is `booked[2][3]`, so a program that reads seats the way people write them subtracts one from each number: `booked[row - 1][seat - 1]`.\n\n### The short way\n\nWhen you know the values as you write the program, list them row by row: braces around each row, commas between the rows, and braces around them all.\n\n```java\nint[][] image = {\n    {0, 3, 7, 3},\n    {3, 8, 9, 8},\n    {0, 3, 7, 3}\n};\nSystem.out.println(image[1][1]);\nSystem.out.println(image[2][2]);\n```\n\n```output\n8\n7\n```\n\nThe first row in braces is row 0, the next one row 1, and so on. Java counts the rows, and the values in each, to make a grid of the right size. As with a one-dimensional array, the braces alone work only in the line that declares the variable.\n","task":"A cinema's smallest room has 4 rows of 6 seats. Two friends booked row 2, seats 3 and 4, as people count them. Fill in the blanks: create the seat map for exactly this room, and book the second friend's seat, next to the first one's. The program prints:\n\n```\nRow 2, seat 3: true\nRow 2, seat 4: true\nRow 2, seat 5: false\nRow 4, seat 6: false\n```\n","kind":"fill","seed":"public class Main {\n    public static void main(String[] args) {\n        boolean[][] booked = new boolean[[[4]]][[[6]]];\n        booked[1][2] = true;\n        booked[1][[[3]]] = true;\n        System.out.println(\"Row 2, seat 3: \" + booked[1][2]);\n        System.out.println(\"Row 2, seat 4: \" + booked[1][3]);\n        System.out.println(\"Row 2, seat 5: \" + booked[1][4]);\n        System.out.println(\"Row 4, seat 6: \" + booked[3][5]);\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        boolean[][] booked = new boolean[4][6];\n        booked[1][2] = true;\n        booked[1][3] = true;\n        System.out.println(\"Row 2, seat 3: \" + booked[1][2]);\n        System.out.println(\"Row 2, seat 4: \" + booked[1][3]);\n        System.out.println(\"Row 2, seat 5: \" + booked[1][4]);\n        System.out.println(\"Row 4, seat 6: \" + booked[3][5]);\n    }\n}\n","hints":["The first size is the number of rows and the second the number of seats in a row: `new boolean[4][6]`.","Row 2, seat 4 as people count is index 1 for the row and index 3 for the seat, one less than each number."],"tests":[{"name":"Output","stdin":"","expect":"Row 2, seat 3: true\nRow 2, seat 4: true\nRow 2, seat 5: false\nRow 4, seat 6: false","hidden":false}],"require":[{"pattern":"new\\s+boolean\\s*\\[\\s*4\\s*\\]\\s*\\[\\s*6\\s*\\]","message":"The room has 4 rows of 6 seats, so the map is `new boolean[4][6]`: the sizes are counts, not the last index."}],"forbid":[],"more":[{"task":"A garden has two tomato beds, and `picked[bed][week]` holds how many tomatoes were picked from a bed in each of three weeks. What does this program print? Keep the row and the column of each cell apart, and remember the default value.\n","kind":"predict","seed":"public class Main {\n    public static void main(String[] args) {\n        int[][] picked = new int[2][3];\n        picked[0][0] = 2;\n        picked[1][2] = 3;\n        picked[0][2] = picked[1][2] + 1;\n        picked[1][0]++;\n        System.out.println(picked[0][0] + picked[0][1] + picked[0][2]);\n        System.out.println(picked[1][0] + picked[1][1] + picked[1][2]);\n        System.out.println(picked[0][1]);\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        int[][] picked = new int[2][3];\n        picked[0][0] = 2;\n        picked[1][2] = 3;\n        picked[0][2] = picked[1][2] + 1;\n        picked[1][0]++;\n        System.out.println(picked[0][0] + picked[0][1] + picked[0][2]);\n        System.out.println(picked[1][0] + picked[1][1] + picked[1][2]);\n        System.out.println(picked[0][1]);\n    }\n}\n","hints":["`picked[0][2]` is row 0, column 2: bed 0's third week. It gets bed 1's third week plus one, so 4.","`picked[1][0]++` adds one to a cell that held the default 0. The cells that are never assigned, such as `picked[0][1]`, stay 0."],"tests":[{"name":"Output","stdin":"","expect":"6\n4\n0","hidden":false}],"require":[],"forbid":[],"lines":["6","4","0"]},{"task":"A cinema's room has 5 rows of 8 seats. Write its booking program. It reads bookings, one per line, until the line `end`. A booking is a row and a seat, counted from 1 as people count them, with a space between them, such as `3 4`. Every row is 1 to 5 and every seat 1 to 8. Keep track of the booked seats in a `boolean[][]`:\n\n- a free seat gets booked, and the program prints `Booked row 3, seat 4`\n- a seat that's already booked stays as it is, and the program prints `Row 3, seat 4 is taken`\n\nAt the end, it prints how many seats were booked. For the input `3 4`, `1 1`, `3 4`, `5 8` and `end` the program prints:\n\n```\nBooked row 3, seat 4\nBooked row 1, seat 1\nRow 3, seat 4 is taken\nBooked row 5, seat 8\nSeats booked: 3\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        boolean[][] booked = new boolean[5][8];\n        int count = 0;\n        while (true) {\n            String line = scanner.nextLine();\n            if (line.equals(\"end\")) {\n                break;\n            }\n            String[] parts = line.split(\" \");\n            int row = Integer.valueOf(parts[0]);\n            int seat = Integer.valueOf(parts[1]);\n            if (booked[row - 1][seat - 1]) {\n                System.out.println(\"Row \" + row + \", seat \" + seat + \" is taken\");\n            } else {\n                booked[row - 1][seat - 1] = true;\n                count++;\n                System.out.println(\"Booked row \" + row + \", seat \" + seat);\n            }\n        }\n        System.out.println(\"Seats booked: \" + count);\n    }\n}\n","hints":["Create the map with `new boolean[5][8]`. For each line, `split(\" \")` gives the row and the seat as text, and `Integer.valueOf` turns each into a number.","Row `row`, seat `seat` as people count is `booked[row - 1][seat - 1]`. If it's `true`, the seat is taken; otherwise set it to `true` and add one to a counter."],"tests":[{"name":"Test 1","stdin":"3 4\n1 1\n3 4\n5 8\nend\n","expect":"Booked row 3, seat 4\nBooked row 1, seat 1\nRow 3, seat 4 is taken\nBooked row 5, seat 8\nSeats booked: 3","hidden":false},{"name":"Test 2","stdin":"end\n","expect":"Seats booked: 0","hidden":true},{"name":"Test 3","stdin":"2 2\n2 3\n2 2\n2 3\n2 4\nend\n","expect":"Booked row 2, seat 2\nBooked row 2, seat 3\nRow 2, seat 2 is taken\nRow 2, seat 3 is taken\nBooked row 2, seat 4\nSeats booked: 3","hidden":true},{"name":"Test 4","stdin":"1 8\n5 1\n1 8\n4 7\n5 1\n1 8\nend\n","expect":"Booked row 1, seat 8\nBooked row 5, seat 1\nRow 1, seat 8 is taken\nBooked row 4, seat 7\nRow 5, seat 1 is taken\nRow 1, seat 8 is taken\nSeats booked: 3","hidden":true}],"require":[{"pattern":"new\\s+boolean\\s*\\[\\s*5\\s*\\]\\s*\\[\\s*8\\s*\\]","message":"Keep the seat map in a `boolean[][]` of 5 rows and 8 seats: `new boolean[5][8]`."}],"forbid":[{"pattern":"\\b(ArrayList|HashMap|HashSet|List|Map|Set)\\b","message":"Keep track of the seats in the two-dimensional array, without a list, set or map."}]}]}