Inserting elements

An unwritten rule of this card game is that the players’ names have to be in alphabetical order. As you can see, the list is missing a player that starts with the letter F. Luckily, Frank has just arrived. You want to add him to the list between Eli and Gina. To do that, you can use the insert(_:at:) method:

players.insert("Frank", at: 5)

The at argument defines where you want to add the element. Remember that the array is zero-indexed, so index 5 is exactly between Eli and Gina.

results matching ""

    No results matching ""