< Unit 5 - Two-Dimensional Arrays ('22-'23)

Lesson 1: Two-Dimensional (2D) Arrays

45 minutes

Overview

What if the data I need to store is best represented as a table?

Students explore the structure of two-dimensional (2D) arrays and identify similarities and differences between 1D and 2D arrays. Students learn to access and modify individual elements by referring to their row and column index values. Students then practice declaring and initializing 2D arrays using the new keyword and initializer lists and work with the elements stored in the arrays.

CSA Conceptual Framework
      • VAR-2.F.1 - 2D arrays are stored as arrays of arrays. Therefore, the way 2D arrays are created and indexed is similar to 1D array objects
      • VAR-2.F.2 - For the purposes of the exam, when accessing the element at arr[first][second], the first index is used for rows, the second index is used for columns.
      • VAR-2.F.3 - The initializer list used to create and initialize a 2D array consists of initializer lists that represent 1D arrays.
      • VAR-2.F.4 - The square brackets [row][col] are used to access and modify an element in a 2D array.

Agenda

Objectives

Students will be able to:
  • Access and modify elements in a two-dimensional (2D) array using row and column indexes
  • Declare and initialize a two-dimensional (2D) array using the new keyword and an initializer list

Preparation

  • Print copies of the Unit 5 Guide (one for each student)
  • Check the Teacher's Lounge for verified teachers on the CSA Forum to find additional strategies or resources shared by fellow teachers

Links

Heads Up! Please make a copy of any documents you plan to share with students.

For the students

Vocabulary

  • column - a vertical (up and down) series of data in a two-dimensional (2D) array
  • inner array - an array that is nested inside another array
  • outer array - the outermost array of a two-dimensional array
  • row - a horizontal (left to right) series of data in a two-dimensional (2D) array
  • two-dimensional (2D) array - an array of arrays often represented as a table with rows and columns

Teaching Guide

Warm Up (5 minutes)

Software Engineering Goals

Remarks

In the previous unit, you expanded your knowledge of conditional statements. You also learned about logical operators as we continued developing your software engineering skills. We will be learning more about how we can work with data in Java programs and continue to use software engineering characteristics and skills to make design decisions and develop more robust algorithms.

Distribute: Give each student a copy of the Unit 5 Guide.

Discuss: Use the Retrieve-Pair-Share strategy to discuss the prompt, then have students note their goals on the Unit 5 Guide.

  • What software engineering characteristics do you want to work on in this unit?
  • How will you know you grew in this area by the end of the unit?

Discussion Goal: Students share characteristics and skills that they want to improve and identify goals.

Teaching Tip

Students can choose any characteristic or skill they want to improve, regardless of whether it is an area for improvement. Students can choose a characteristic that is a strength that they want to further develop as an anchor for upcoming units, or they can choose a characteristic that is an area for improvement.

Activity (35 minutes)

Two-Dimensional (2D) Arrays (10 minutes)

Remarks

We have used 1D arrays to store primitive values and object references as a list of data. Sometimes we need a way to represent similar data as a table rather than a list. Luckily, Java has a way for us to do this!

Do This: Review the lesson objectives.

Do This: Direct students to Level 1 on Code Studio to investigate the program with a partner. Students make the changes to the program as prompted.

Discuss: Click through the animated slide to display the prompts.

  • What do you notice about the code in this program?
  • What do you wonder about the code in this program?

Discussion Goal: Students notice the array has two sets of brackets instead of one like they have used with 1D arrays. Students also notice that the data prints in rows of values. Students may wonder what the values in each set of brackets reference or the benefits of this type of data structure.

Display: Show the video – Two-Dimensional (2D) Arrays.

Remarks

A two-dimensional, or 2D, array is similar to a 1D array but with one important difference – a 2D array is an array of arrays!

Do This: Click through the animated slide to explain how objects are stored in an array.

Do This: Click through the animated slide to explain the structure of a 2D array.

Do This: Explain that 2D arrays are often represented as a table with rows and columns.

Do This: Click through the animated slide to demonstrate declaring and initializing a 2D array using the new keyword.

Do This: Click through the animated slide to demonstrate declaring and initializing a 2D array using an initializer list.

Adding Elements to a 2D Array (10 minutes)

Remarks

Adding elements to a 2D array is similar to how we added elements to a 1D array.

Discuss: Use the Retrieve-Pair-Share strategy to discuss the prompt.

  • Based on what you know about 1D arrays, how do you think we would access an element in a 2D array?

Discussion Goal: Students recall that with a 1D array, they specified an index for the position in the list. Students suggest using the row and column indexes to specify the position in a 2D array.

Teaching Tip

If students need help making the connection, ask additional guiding questions. For example,

  • What did the value we specified inside the brackets for a 1D array represent?
  • How do we find the position of an element in a 2D array?

Display: Show the video – Working with 2D Array Elements.

Do This: Click through an animated slide to demonstrate how to access values from a two-dimensional array.

Do This: Have students respond to the self-check question. Click through the animated slide to reveal the correct answer and discuss it as a class.

Using 2D Arrays (15 minutes)

Remarks

2D arrays can be more useful for some scenarios when we need to organize our data as a table instead of as a list. While some things are different from 1D arrays, 2D arrays also have some similarities.

Discuss: Use the Hold That Thought strategy to discuss the prompt.

  • What are the similarities and differences between 1D and 2D arrays?
  • How would a 2D array make it easier to store this data?

Discussion Goal: Students identify that both 1D and 2D arrays are immutable, so the data type and size cannot be changed after it has been initialized. Students also note that 2D arrays use a similar syntax as 1D arrays. Elements in a 2D array are accessed by referencing the row and column location, while elements in a 1D array are accessed by referencing the position in the list. Students suggest that a 2D array makes it easier to store the example data because each row represents values for a specific category, and elements can be accessed as a table.

Teaching Tip

Have students recall the characteristics and syntax of a 1D array to help them identify the similarities and differences between 1D and 2D arrays. You can also ask students how storing the example data in a 1D array would be more challenging when getting information for a specific row or column. For example,

  • Consider the standard algorithms we implemented for 1D arrays. How would we use those standard algorithms to find the sum of just one category if this was instead stored as a 1D array?

Do This: Direct students to Level 2 on Code Studio to complete Levels 2, 3, and 4. Students complete a Check for Understanding on Level 2, then continue to complete a choice level on Level 3 to create a 2D array using the new keyword. On Level 4, students complete a choice level to create a 2D array using an initializer list using the console or The Theater.

Wrap Up (5 minutes)

But, Because, So

Remarks

We have learned a lot about 2D arrays and how they are similar and different from 1D arrays!

Distribute: Give each student a sticky note or scrap piece of paper.

Do This: Have students respond to the prompt on the sticky note or scrap piece of paper.

Teaching Tip

If time permits, have students share their responses with a neighbor.

Do This: Review the concepts covered in this lesson.

Display: Key Vocabulary


Assessment: Check for Understanding

Check For Understanding Question(s) and solutions can be found in each lesson on Code Studio. These questions can be used for an exit ticket.

AP Classroom Topic Questions

To assign questions from the AP Classroom Question Bank that align with this lesson, create a custom quiz in AP Classroom by searching the Question Bank for the Essential Knowledge statements listed at the top of this lesson plan. You can find instructions and video demonstrations to do this on AP Central.

Creative Commons License (CC BY-NC-SA 4.0).

This work is available under a Creative Commons License (CC BY-NC-SA 4.0).

If you are interested in licensing Code.org materials for commercial purposes contact us.