This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. empty array initialization | |
let numbers = [Int]() | |
let nums: [Int] = [] | |
let newNumbers = Array<Int>() | |
let newNums: Array<Int> = Array() | |
// 2. preinitializing a fixed number of default values | |
let repeatSwift = Array<String>(repeating: "Swift", count: 5) // ["Swift", "Swift", "Swift", "Swift", "Swift"] | |
// or | |
let repeatSwift = Array(repeating: "Swift", count: 5) // ["Swift", "Swift", "Swift", "Swift", "Swift"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let array = ["Hello", "people", "of", "the", "world"] | |
// looping using count property | |
for ind in 0..<array.count { | |
print(ind) | |
} | |
// 0, 1, 2, 3, 4 | |
// looping using the endIndex property | |
for ind in 0..<array.endIndex { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. | |
class CircularBuffer<T> { | |
var bufferMaxSize: Int | |
var size = 0 | |
// 2. | |
var elements = Array<T?>(repeating: nil, count: 8) | |
// 3. | |
init(maxSize: Int) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CircularBuffer<T> { | |
// ... | |
// 1. | |
var front: T? { | |
if isEmpty() { | |
return nil | |
} else { | |
// 2. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CircularBuffer<T> { | |
// ... | |
func enqueue(item: T) { | |
// 1. | |
if isEmpty() { | |
elements[bufferMaxSize-1] = item | |
} else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func dequeue() -> T? { | |
// ... | |
// 1. | |
if !isEmpty() { | |
// 2. | |
let front = elements[bufferMaxSize - size] | |
elements[bufferMaxSize - size] = nil | |
size -= 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CircularBuffer<T> { | |
var bufferMaxSize: Int | |
var size = 0 | |
var elements = Array<T?>(repeating: nil, count: 8) | |
var front: T? { | |
if isEmpty() { | |
return nil | |
} else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. empty set initialization | |
var numbers = Set<Int>() | |
var nums: Set<Int> = Set() | |
// 2. empty set initialization with array literal | |
var numbersSet: Set<Int> = [] | |
// 3. creating set with predefined elements using array literals | |
var newNums: Set = [1, 2, 3, 4] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var numbers = Set<Int>() | |
numbers.insert(3) // (true, 3) | |
numbers.insert(22) // (true, 22) | |
numbers.insert(22) // (false, 22) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var numbers = Set<Int>(minimumCapacity: 30) | |
numbers.capacity // 48 |