Lesson 5: Lists of Objects
45 minutes
Overview
Why does an ArrayList
only store objects?
Students explore generic types in Java and learn why an ArrayList
can only store objects. Students then practice applying the algorithms they learned with 1D and 2D arrays to ArrayList
s to find information and perform calculations with ArrayList
data.
Standards
VAR-2 - To manage large amounts of data or complex relationships in data, programmers write code that groups the data together into a single data structure wihtout creating individual variables for each value.
VAR-2.D - Represent collections of related object reference datat using ArrayList objects.
- VAR-2.D.3 - Java allows the generic type ArrayList<E>, where the generic type E specifies the type of the elements.
- VAR-2.D.4 - When ArrayList<E> is specified, the types of the reference parameters and return type when using the methods are type E.
- VAR-2.D.5 - ArrayList<E> is preferred over ArrayList because it allows the compiler to find errors that would otherwise be found at run-time.
Agenda
Objectives
Students will be able to:
- Explain the functionality of generic types in Java
- Implement standard algorithms to find information and perform calculations with
ArrayList
data
Preparation
- Create code review groups if you are not reusing the same groups
- 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
- U6L5 Extra Practice - Handout
Vocabulary
- Generic Type - Allows a class, or type, to be used as the parameter to an
ArrayList
and is indicated by< >
Teaching Guide
Warm Up (5 minutes)
ArrayList Syntax
Remarks
We have worked with 1D and 2D arrays in previous units and have been learning about ArrayList
s over the last few lessons. While an ArrayList
allows us to store a list of data similar to a 1D array, it has additional capabilities that make it more flexible than a 1D array. It also uses a different syntax than a 1D array.
Discuss: Click through the animated slide to display the prompts. Use the Retrieve-Pair-Share strategy to discuss the prompts.
- What is unique about the syntax of an
ArrayList
? - What do you think is the purpose of the angle brackets
< >
?
Discussion Goal: Students note that the angle brackets < >
are unique to ArrayList
s. Students recall that the type of data the ArrayList
can store is specified inside the angle brackets and guess that the purpose of the angle brackets is to specify the reference type.
Activity (30 minutes)
Arrays of Objects to ArrayLists (15 minutes)
Remarks
There is a reason why an ArrayList
uses this syntax. We have learned that an ArrayList
can only store objects, like Integer
, Double
, String
, and any other object we choose.
Do This: Review the lesson objectives.
Group: Place students in pairs.
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 that an ArrayList
can store objects like a 1D and 2D array. Students also notice that an ArrayList
can be declared and initialized without the < >
but doing so allows the ArrayList
to store objects of different and unrelated types. Students may wonder why the < >
are used instead of the [ ]
and why omitting the < >
allows the ArrayList
to store different and unrelated objects.
Do This: Click through the animated slide to define generic type and explain the functionality of ArrayList<E>
.
ArrayLists and Algorithms (15 minutes)
Remarks
We have developed and reused standard algorithms to work with 1D and 2D arrays to solve different types of problems. Let’s combine our knowledge of standard algorithms with what we’ve learned about ArrayList
s.
Do This: Direct students to Level 2 on Code Studio to complete Levels 2 and 3. On Level 2, students translate the given code with a 1D array to an ArrayList
. On Level 3, students complete a choice level to apply standard algorithms to an ArrayList
.
Do This: Click through the animated slide to have students participate in the Code Review Call and Response.
Do This: Direct students to complete a code review on Level 4.
Wrap Up (10 minutes)
Closing the Loop
Remarks
We have learned a lot about ArrayList
s and applying standard array algorithms to ArrayList
s. Let's recap some of the key concepts we have learned.
Discuss: Click through the animated slide to display the prompts.
- When is it best to use an
ArrayList
instead of a 1D array? - How is applying standard algorithms to an
ArrayList
similar to a 1D array? How is it different? - What were you confident about in this lesson? What would you like to practice?
Discussion Goal: Students share scenarios where an ArrayList
is best, such as when the size of the list is unknown and may change. Students identify similarities and differences between applying standard algorithms to an ArrayList
and a 1D array, such as using the same steps but different syntax. Students share concepts they are confident about and that they need more practice.
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.
The following Topic Questions in AP Classroom can be assigned as a formative assessment for this lesson:
- Topic Questions 7.1
Note: Some Learning Objectives and Essential Knowledge statements in the suggested Topic Questions are covered in later units.
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.