List - Construction
Not Started

Sometimes life is hard and we're starting from scratch. We can also create an empty List and add elements later.

We'll swap out the {} syntax we saw in the last lesson and use the () syntax to initialize an empty List:

List <String> friends = new List<String>();

The () syntax is actually a constructor! You can leave it empty and initialize without data, or you can use it to pass in another list.

List <String> astrosFriends = new List<String>{'Codey', 'Ruth', 'Flo'}; List<String> myFriends = new List<String>(astrosFriends); // Astro's friends are our friends!

We're able to pass astrosFriends into the constructor of myFriends, which instructs myFriends to be initialized with all of the elements present in astrosFriends.

Here's a summary of the multiple ways to initialize a List:

List<String> myList; // not initialized, myList is null List<Decimal> opportunityAmounts = new List<Decimal>(); // Empty list List<Integer> opportunityStages = new List<Integer>{0, 1, 2, 3, 4, 5, 6, 7}; List<Integer> clonedOpptyStages = new List<Integer>(opportunityStages);

Challenge

Create a new List of Strings named cityListCopy and initialize it by passing cityList into its constructor.