Agenda • Warmup • Lesson 3.3 (2-D Arrays) • Independent Practice (3.3 Assignments) • Time Permitting: Review of 1-dimensional arrays; more practice • Closure Activity Students will be able to: • Understand what a 2-dimensional array is • Declare an 2-d array, initialize it, and display it • Use nested for loops to traverse a 2-d array • Create programs that feature rows/columns based on a 2-d array • See how today's lesson fits into the unit and the course as a whole Warmup 1) 2) 3) 4) 5) 6) 7) Why are arrays useful? Can an array hold Strings? Can an array hold objects? Declare an array that will hold 6 students’ GPAs. Is the previous questions’ punctuation correct? Is the previous question’s punctuation correct? Declare and initialize an array that holds these 2 words: false and true. 8) Write the first line only of a for-each loop that will traverse an array that holds letters. 9) Write the constructor of a child class called Car. It receives one int parameter and sends it to its parents constructor. 10) Is the previous instruction’s punctuation correct? Initializing an array • When an array holds primitive types (int, double, char, boolean), if it is not initialized, then its values will be set to their default (0 for ints, false for booleans, blank for chars) • However, since String is an object, its default is not simply blank. Instead, its default is null). • Demo: ArrayInitialize 2-dimensional Arrays • A 2-dimensional array has rows and columns • Each row is itself a 1-D array • A Tic-Tac-Toe board is an example of a 3 by 3 2-d array • Syntax: type[][] name = new type[rows][columns] • A loop inside a loop is called a nested loop • Almost always, we use nested for-loops to traverse a 2-d array • Demo2dArray • If there is a 2-D array called board, board.length = the length of each column = how many rows board[0].length = the length of the 1st row = how many columns Row-major order vs column-major order • When you traverse (go through) a 2D array, if you go through the first row from left to right, then the 2nd row from left to right, and so on, this is called row-major order. • Going through the first column, up to down, then the 2nd column, and so on, is called column-major order. 2-D arrays As Parameters • 2-d arrays can be passed as a parameter or returned from a method • Syntax: public void setBoard(String[][] ticTacToe) // receives a 2-d array as a parameter or… public String[][] getBoard( ) // this method would return a 2-d array to the client Using for-each loops with 2D arrays • Remember, a 2D array is really just a 1D array in which each element is itself a 1D array. • This is important, because when using a foreach loop with a 2D array, you first place each row of the table into a 1D array. • Then, with a nested for-each loop, you assign each element of that row to a variable. • Demo: ForEachLoopDemo • Important: do not use a for-each loop to populate an array. Only use it to access the elements inside the array. Assignment #1 (Alpha) • Create and display a 9-by-16 table that holds chars. • Place the letter k in each “cell.” • Display the table. • Now, place a randomly-generated lowercase letter in each cell. • Display the table again. • Hints… – RandomNumberDemo – Ascii Assignment #2 (Classroom) • Create a classroom diagram: 5 rows, 6 columns. • Place a dash in each cell. • There are 12 students in the class. Ask the user where each student wants to sit, and then place an ‘s’ in that spot. • After each student is placed, display the classroom. • Error checking: – Don’t allow the user to choose a spot that’s already taken. – Prevent OutOfBounds exceptions. AT ALL COSTS. Assignment #3 (Connect3) • You know the game Connect 4. Create a version of this game called Connect 3: it uses a 3 by 4 table, and the first player to connect 3 in a row (horizontal, vertical, or diagonal) wins. • Remember, it is a 2-player game, and each player chooses a column to drop a piece into. “Gravity” pulls each piece to the lowest spot available. • Allow the user to play multiple times if they want to.
© Copyright 2026 Paperzz