CS49J Lesson 12 – ArrayLists
Ch 7: 7.7

When you come in
- Connect to the Internet
- Log in to Piazza
- Start Eclipse or your IDE
- Navigate to laughton.com/obrien/sjsu/
cs49j/lessons and open this lesson
Concept: Declaring an ArrayList
Concept: Adding to an ArrayList
Concept: More ArrayList methods
- Let's go back to out professors list: Contents: [Heller, Kim, O'Brien]
- To get the element at index i
String name = professors.get(i);
- To get the number of elements
int size = professors.size()
- The last element is at size() - 1
- To get the last element
String last = professors.get(professors.size() - 1);
- To remove the element at index 1
String name = professors.remove(1);
- Contents of professors: [Heller, O'Brien]
- Contents of name: Kim
Concept: Enhanced for loop
Activity - Using an ArrayList
Record your participation in Piazza clicker question Lesson12 Q1
- Write an application that declares an array list of Strings called flowers
- To the array list, add: rose, violet, pansy, zinnia
- Insert lavender in the next to the last position
- Remove element at index 1.
- Now replace each element so it start with an uppercase letter
- You need to use a traditional for loop since you are changing the ArrayList
- Print the array list on one line without using a loop (page 345. section 7.7.1)
- What is printed?
- [rose, violet, pansy, zinnia]
- [Rose, Violet, Pansy, Lavender, Zinnia]
- [Rose, Pansy, Lavender, Zinnia]
- [Rose, Violet, Pansy, Lavender]
Activity - ArrayList of objects: sum
Record your participation in Piazza clicker question Lesson12 Q2
- We are going to work with an ArrayList of Circle objects.
- Download the starter file. It contains a Circle class, a CircleManager class (started), and a CircleManagerTester
- Add a method sumAreas that returns the sum of all the areas or 0 if the ArrayList is empty.
- Run the tester
- Got it
- Got it but it was hard
- Couldn't do it in the time allowed
- Didn't know where to start
Activity - ArrayList of objects: average
Record your participation in Piazza clicker question Lesson12 Q3
- Add a method averageArea to return the average area or 0 if array list is empty.
- Do not calculate the sum in this method (use the previous method)
- Uncomment the code in the tester that prints the average and run the tester
- Got it
- Got it but it was hard
- Couldn't do it in the time allowed
- Didn't know where to start
Activity - ArrayList of objects: Largest
Record your participation in Piazza clicker question Lesson12 Q4
- Add a method to CircleManager to get the largest Circle. If the ArrayList is empty, return null
- Uncomment the code in the tester that prints the Circle and run it
- Got it
- Got it but it was hard
- Couldn't do it in the time allowed
- Didn't know where to start
Activity - ArrayList of objects: find all matches
Record your participation in Piazza clicker question Lesson12 Q5
- Add a method that returns an ArrayList containing all the Circles with radius greater than or equal to 10. Call it radiusGreaterThan10.
- Add code to the tester to print the ArrayList
- Got it
- Got it but it was hard
- Couldn't do it in the time allowed
- Didn't know where to start
Activity - ArrayList of objects: find first match
Record your participation in Piazza clicker question Lesson12 Q6
- Add a method to get the first Circle with a radius greater than 10. Call it firstRadiusGreaterThan10. If the ArrayList is empty or does not have an Circle with a radius greater than 10, return null
- Uncomment the code in the tester that prints that Circle
- Got it
- Got it but it was hard
- Couldn't do it in the time allowed
- Didn't know where to start