From the course: Java Cheat Codes: Most Popular Functions
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Solution: ArrayList.add(Object obj) - Java Tutorial
From the course: Java Cheat Codes: Most Popular Functions
Solution: ArrayList.add(Object obj)
(upbeat music) - [Instructor] Now let's walk through the solution for the ArrayList add method. I have my solution between lines 11 to round 36, and I'll just walk through what we're seeing here, so starting at the beginning, lines 11 through 13, we are declaring private properties, so these are accessible only within this class. With String name, I am storing the name of the person, with int age, I'm storing the age of the person, and then, with private ArrayList String additionalAnswers, I am storing the additional answers as a list, and then, here, lines 15 to 18, I am creating a constructor to create Answer objects, so for this.name, I am assigning the provided name to the object's name property, this.age = age, I am assigning the provided age to the object's age property, and then, for this.additionalAnswers = new ArrayList, I am creating an empty ArrayList for additionalAnswers. Lines 21 through 23, what we're doing here is a method to add an additional answer to the list, so I…