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
How would you enumerate each item of the shoppingList array?
for (index, value) in shoppingList.enumerate() { print("Item \(index + 1): \(value)") } // Item 1: Six eggs // Item 2: Milk // Item 3: Flour // Item 4: Baking Powder // Item 5: Bananas
In sets, do items appear once or more than once?
Once.
What is a hash value?
An Int value that is the same for all objects that compare equally, such that if a = b, it follows that a.hashValue == b.hashValue.
Are all of Swift’s basic types (String, Int, Double, Bool) hashable by default?
Yes.
How is a type of a set written?
Set where Element is the type that the set is allowed to store.
Create an empty set of a certain type (Character) using initializer syntax for the variable “letters”.
var letters = Set()
OR
letters = []
Can you initialize a set with an array literal as a shorthand way to write one or more values as a set collection?
Yes.
How would you create a set (“favoriteGenres”) consisting of Rock, Classical, and Hip Hop with an array literal?
var favoriteGenres: Set = [“Rock”, “Classical”, “Hip hop”]
Can a set type be inferred from a literal array alone?
No. The type must be explicitly declared.
How do you access and modify a set?
Through its methods and properties.
How would you check to see if favoriteGenres includes Funk?
if favoriteGenres.contains(“Funk”)
How do you iterate over the values in a set?
With a for-in loop.
for genre in favoriteGenres { print("\(genre)") } // Classical // Jazz // Hip hop
How would you iterate over the values of a set in a specific order?
for genre in favoriteGenres.sort() { print("\(genre)") } // Classical // Hip hop // Jazz
Name four fundamental set operations.
Intersect, exclusiveOr, union, subtract.
What method creates a new set with only the values common to both sets?
.intersect(_:)
What method creates a new set with values in either set, but not both?
.exclusiveOr(_:)
What method creates a new set with all of the values in both sets?
.union(_:)
What method creates a new set with values not in the specified set?
.subtract(_:)
What is a superset?
A set that contains all the elements of another set and of itself.
What is a subset?
A set that’s elements are also contained in another set.
What are disjoint sets?
Two sets that do share no common elements.
What method determines whether all of the values of a set are contained in the specified set?
.isSubsetOf(_:)
What method determines whether a set contains all of the values in a specified set?
.isSupersetOf(_:)
What methods determine whether a set is a subset or superset, but not equal to, a specified set?
.isStrictSubsetOf(_:)
.isStrictSupersetOf(_:)
When do you use a dictionary?
When you need to look u values based on their identified, in much the same way that a real-world dictionary is used to look up the definition of a particular word.
Create an empty Dictionary (namesOfIntegers) of a String type by using initializer syntax.
var namesOfIntegers = Int: String
Can Swift infer set type information?
Yes, it can.
Create a namesOfIntegers empty dictionary with an empty dictionary literal.
namesOfIntegers = [:]
Create a dictionary to store the names of international airports with a key standing for the airport’s three-letter code and the value as the airport name.
var airports: [String: String] = [“YYZ”: “Toronto Pearson”, “DUB”: “Dublin”]
What dictionary method sets or updates the value for a particular key?
.updateValue(_:forKey:)
The .updateValue(_:forKey:) method returns what of the dictionary’s value type?
An optional value (e.g., String? or “optional String” with the optional string containing the old value for that key if one existed before.)
You can remove a key-value pair from a dictionary by assigning what value for that key?
nil
What method removes a key-value pair from a dictionary?
.removeValueForKey(_:)
How can you iterate over the key-value pairs in a dictionary?
With a for-in loop.
for (airportCode, airportName) in airports {
print("\(airportCode): \(airportName)") } // YYZ: Toronto Pearson // LHR: London Heathrow
How can you retrieve an utterable collection of dictionary’s keys or values?
By accessing its keys and values properties.
for airportCode in airports.keys {
print(“Airport code: (airportCode)”)
}
// Airport code: YYZ // Airport code: LHR
for airportName in airports.values {
print(“Airport name: (airportName)”)
}
// Airport name: Toronto Pearson // Airport name: London Heathrow
If you need to use a dictionary’s keys or values with an API that takes an array instance, what should you do?
Initialize a new array with the keys or values property.
let airportCodes = [String](airports.keys) // airportCodes is ["YYZ", "LHR"]
let airportNames = [String](airports.values) // airportNames is ["Toronto Pearson", "London Heathrow"]
What method should you use to iterate over the keys or values of a dictionary in a specific order?
.sort()