List - Clear and Remove
Not Started

There are some scenarios where you'll need to clear your list of all values or remove a specific value. There are several instance methods that can help you do that.

Consider you have a list of email addresses:

List<String> emailAddressList = new List<String>{'jerry@campapex.org', 'elaine@campapex.org'}; emailAddressList.add('george@campapex.org'); // emailAddressList is jerry@campapex.org, elaine@campapex.org, george@campapex.org

and you want to remove elaine@campapex.org. There are a few ways to do this, depending on what you know about your list.

If you already know elaine@campapex.org is in the first index, you can simply use the remove(index) instance method to remove an element based on it's index:

List<String> emailAddressList = new List<String>{'jerry@campapex.org', 'elaine@campapex.org'}; emailAddressList.add('george@campapex.org'); emailAddressList.remove(1); // emailAddressList is now just jerry@campapex.org, george@campapex.org

But if you don't know where elaine@campapex.org is in your list, you can use the indexOf(element) method to find out. The output of this method can be passed in as an input to the remove(index) method.

List<String> emailAddressList = new List<String>{'jerry@campapex.org', 'elaine@campapex.org'}; emailAddressList.add('george@campapex.org'); Integer elaineIndex = emailAddressList.indexOf('elaine@campapex.org'); emailAddressList.remove(elaineIndex); // emailAddressList is now just jerry@campapex.org, george@campapex.org

Now regardless of where elaine@campapex.org is in the List, it will be found and removed.

There's a small catch here. If elaine@campapex.org is not in the list, indexOf() returns -1. But the List doesn't have an index of -1, it's index starts at 0.

So if our code looked like this:

List<String> emailAddressList = new List<String>{'jerry@campapex.org'}; Integer elaineIndex = emailAddressList.indexOf('elaine@campapex.org'); // returns -1 emailAddressList.remove(elaineIndex); // Runtime Error: System.ListException: List index out of bounds: -1

Our program would crash. In the next course, we'll show you how to get around this using conditional statements.

Other times, you don't want to remove just one element - you want to remove all elements and clear your list. Well, guess what? You won't guess! You can't guess! But maybe you already did. There's a method called clear(). They thought of everything!

This method is super simple.

List<Integer> yearsOfEmployment = new List<Integer>{12, 6, 3, 2, 2, 1, 1}; Integer listSize = yearsOfEmployment.size(); // returns 7 yearsOfEmployment.clear(); listSize = yearsOfEmployment.size(); // returns 0

You can also clear a list be re-initializing it like this:

List<Integer> yearsOfEmployment = new List<Integer>{12, 6, 3, 2, 2, 1, 1}; Integer listSize = yearsOfEmployment.size(); // returns 7 yearsOfEmployment = new List<Integer>(); listSize = yearsOfEmployment.size(); // returns 0

Challenge

The removeCountry method accepts two parameters. A List of countries and a country that must be removed. Use the methods from this lesson to find the index of the element and remove it from the list.

Code similar to this may call your code:

List<String> countryList = new List<String>{'Mexico', 'Colombia', 'Argentina', 'Peru'}; CountryController cc = new CountryController(); cc.removeCountry(countryList, 'Peru');