List - Add Elements
Not Started

Alright so let's be real, we're not always going to have the data we need to initialize a List. More often than not, we'll need to add elements to our list after initializing.

To do so, we'll use the add(element) instance method. The method's parameter must match the datatype of the list and will be appended to the end of the list.

// Starting with an empty list and then adding elements List<Decimal> amountList = new List<Decimal>(); amountList.add(50.45); amountList.add(320.85); Decimal secondAmount = listOfAmounts[1]; //320.85 // Initializing a list with a couple of elements, then adding more List<String> nameList = new List<String>{'Jerry', 'George'}; nameList.add('Elaine'); nameList.add('Kramer'); String firstNameInList = nameList[0]; // Jerry String thirdNameInList = nameList[2]; // 'Elaine'

Now, if you're not careful and pass in the wrong datatype:

// Starting with an empty list and then adding elements List<Decimal> amountList = new List<Decimal>(); amountList.add(50.45); amountList.add(320.85); amountList.add('500.25'); // Compilation Error!

You'll be stopped with a compilation error: Method does not exist or incorrect signature: void add(String) from the type List<Decimal>.

So watch out!

Challenge

Given the List of StringscampingList, use the add() method to append the following values: Tent, Snacks, Flashlight.

Code similar to this will call your method:

List<String> campingList = new List<String>(); CampingListController campingController = new CampingListController(); campingController.addBasics(campingList);