Collection Types Flashcards
What are Swift’s three primary collection types?
Arrays, sets, and dictionaries.
What do collection types collect?
Values.
What is an array?
An ordered collection of values.
What is a set?
An unordered collection of unique values.
What is a dictionary?
Unordered collections of key-value associations.
If assigned to a variable, are collections mutable or immutable?
Mutable, it’s size and contents can be changed.
If assigned to a constant, are collections mutable or immutable?
Immutable, it’s size and contents cannot be changed.
An array stores values of the _____ type in an _______ list.
Same, ordered.
In an array, can the same value appear multiple times at different positions?
Yes.
How do you write the type of array?
Array where element is the type of value the array is allowed to store.
How do you create an empty array of a certain type using initializer syntax? (Give an example using someInts as the var)
var someInts = [Int]() someInts = [] (if there is already context)
Create an array by adding two arrays together.
Add name of first array variable and second array variable together. EX: var sixDoubles = threeDoubles + anotherThreeDoubles
What is an array literal?
A shorthand way to write one or more values as an array collection. EX: var shoppingList: [String] = [“Eggs”, “Milk”]
You can access and modify an array by using what kind of syntax?
Subscript. EX: var firstItem = shoppingList[0]
Name two ways to add an item to the end of an array.
Use the method .append(_:)
nameOfArray += [“Item to add”]
The first item in an array has a value of what?
Zero.
Use subscript syntax to change the first item in the shoppingList array to “Six eggs”.
shoppingList[0] = “Six eggs”
How do you replace a range of values in an array?
shoppingList{4…6] = [“Bananas”, “Apples”]
Can you use subscript syntax to append a new item to the end of an array?
No.
How would you insert “Maple Syrup” to value zero in the shoppingList array?
shoppingList.insert(“Maple Syrup”, atIndex: 0)
How would you remove “Maple Syrup” from value zero in the shoppingList array?
let mapleSyrup = shoppingList.removeAtIndex(0)
How would you remove the last item in the shoppingList array (“apples”)?
let apples = shoppingList.removeLast()
How would you iterate over the entire set of values in the shoppingList array?
for item in shoppingList { print(item) } // Six eggs // Milk // Flour // Baking Powder // Bananas