Download presentation
Presentation is loading. Please wait.
1
Review of Previous Lesson
24/04/2019 Review of Previous Lesson State as many Vocabulary words and Learning Objectives that you remember from the last lesson as you can. Remember to grade yourself from
2
24/04/2019 Arrays 2-d
3
Language Features and other Testable Topics
24/04/2019 Tested in the AP CS A Exam Notes Not tested in the AP CS A Exam, but potentially relevant/useful Arrays 2-dimensional rectangular arrays, row-major order of 2-dimensional array elements 8
4
Language Features and other Testable Topics
24/04/2019 Notes: 8. Students need to understand that 2-dimensional arrays are stored as arrays of arrays. For the purposes of the AP CS A Exam, students should assume that 2-dimensional arrays are rectangular (not ragged) and the elements are indexed in row-major order. For example, given the declaration int[][] m = {{1, 2, 3}, {4, 5, 6}}; m.length is 2 (the number of rows), m[0].length is 3 (the number of columns), m[r][c] represents the element at row r and column c, and m[r] represents row r (e.g., m[0] is of type int[] and references the array {1, 2, 3}). Students are expected to be able to access a row of a 2-dimensional array, assign it to a 1- dimensional array reference, pass it as a parameter, and use loops (including for-each) to traverse the rows. However, students are not expected to analyse or implement code that replaces an entire row in a 2-dimensional array, such as int[] a = {7, 8, 9}; m[0] = a; // Outside the Subset
5
Multidimensional Arrays
24/04/2019 Does Java have 2D arrays? array2d: If you create an array as below you can think of it as a “matrix” with 3 rows & 4 columns. int[][] array2d = { {1, 0, 12, -1}, {7, -3, 2, 5}, {-5, -2, 2, -9} }; array2d: = reference/address 1 12 -1 7 -3 2 5 -5 -2 2 -9 But in reality, array2d holds an reference to an array of 3 items, where each item is a reference to an array of 4 ints. Note: The declaration & initialisation above can be written on one line, it is written on multiple lines to make it easier to read.
6
Row-major order e.g. array2dName [ row ][ col ] = ….
24/04/2019 Row-major order a00 a01 a02 a03 a10 a11 a12 a13 a20 a21 a22 a23 array2dName [ row ][ col ] = …. e.g. array2d [0][1] is 0 array2d [2][3] is -9 ? ?
7
More Complex Way to create/instantiate a 2d array:
24/04/2019 More Complex Way to create/instantiate a 2d array: The previous simple declaration, creation & initialisation and the more complex way to create/instantiate a 2d array have the same issues as described in the previous presentation on 1d arrays. e.g. int[][] array2d = new int [3][4]; 2 nested loops can then be used to populate the 2d array. Note that this can also be done on 2 separate lines: int[][] array2d; ….. array2d = new int [3][4];
8
Size of a 2d array … .length = the number of rows
24/04/2019 Size of a 2d array … .length = the number of rows e.g. array2d.length = 3 … [0].length = the number of columns in row 0 Note: 2d arrays can be ragged (different number of columns in each row), so this may not give the same number in different rows. However, the AP Computer Science syllabus states “For the purposes of the AP CS A Exam, students should assume that 2-dimensional arrays are rectangular (not ragged) …”. e.g. array2d[0].length = 4
9
Accessing rows of a 2d array
24/04/2019 Accessing rows of a 2d array e.g. array2d[0] is of type int[] and references the array {1, 0, 12, -1} It is also possible to replace an entire row in a 2d array but the AP Computer Science syllabus states that this will not be expected in the exam: e.g. array2d: 7 8 9 10 -3 2 5 -5 -2 -9 int[][] array2d = { {1, 0, 12, -1}, {7, -3, 2, 5}, {-5, -2, 2, -9} }; int[] array1d = {7, 8, 9, 10}; array2d[0] = array1d; // Outside the Subset
10
Nested for loop to iterate a non-ragged 2D array:
24/04/2019 for (int row = 0; row < arr2D.length; row++) { // assuming arr2D is not ragged (different number of columns in each row) for (int col = 0; row < arr2D[0].length; col++) { System.out.print(arr2D[row, col]); } System.out.println(); // If you already ‘know’ the number of rows & columns: for (int rowIndex = 0; rowIndex < rows - 1; rowIndex++) { // or use an int literal e.g. 8 for (int colIndex = 0; colIndex < cols - 1; colIndex++) { // or use an int literal e.g. 8 System.out.println(arr2D[rowIndex, colIndex]); ?
11
Nested For Each loop to iterate a 2D array:
24/04/2019 Nested For Each loop to iterate a 2D array: ? for (int[] arr1D : arr2D) { //Basically pulls out each row. /*Basically now a 1D array (as previously), but in reality, each row of the 2D array above.*/ for (int i : arr1D) { System.out.print(i); } System.out.println();
12
24/04/2019 Write your own programs: Write your own programs from “scratch”. Of course you should use previous programs for reference, but write your code from “scratch” (do not copy and paste).
13
Store BIKE IDs 24/04/2019 Super Bikes owns a rectangular parking area with 30 rows; each row has 4 bike spaces. Each bike is always parked in the same space. The array bikeSpace[30,4] stores the bike registrations. Soni uses a flowchart to help him design a module to populate the array with the bike registrations. Input is terminated using the rogue value “BK000”. Write this program and allow the user to see the contents of the array.
14
Chess Board 1 Liliane wants to write a program to play chess.
24/04/2019 Chess Board 1 Liliane wants to write a program to play chess. She will represent the board of 4 x 4 squares, using a 2-dimensional array. If a chess piece is on a square, it will take a value of 1. Write a program to accept row and column numbers and place a 1 at this position and re-display the board. To display the initially empty board:
15
24/04/2019 Chess Board 2 Produce a different version of the previous “Chess Board 1” program. Liliane's next task is to indicate that there are pieces occupying the first two rows of the 4 x 4 board. Each square in rows 1 and 2 will be given the value 1. Add initial code that occupies the first two rows of the 4 x 4 board with 1’s and clears the other rows. This is all this version needs to do.
16
24/04/2019 Chess Board 3 Produce a new version of the previous “Chess Board 2” program that uses While Loops (as you probably used for loops originally). Occupies the first two rows of the 4 x 4 board with 1’s and clears the other rows but with While Loops
17
Tile Designs 24/04/2019 Ahmed combines white tiles with tiles of one other colour to make a pattern. He draws a design. Here is one example: Ali stores the design in a 2-dimensional array, floorDesign. The length and width of the room will be no more than 9 tiles each. Write a program for Ahmed which: Initially, makes every tile white. Asks the user to enter the size of a design. e.g. 2 tiles by 3 tiles Asks the user to enter the design e.g. Note that with this method the user will enter each row of the design and be expected to know to press the Enter key after each row. If you can think of a better way please do so. Then calculates the number of white tiles and the number of coloured tiles in the design.
18
4/24/2019 Grade yourself Grade yourself on the vocabulary and learning objectives of the presentation.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.