-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy patharrays.swift
71 lines (52 loc) · 1.74 KB
/
arrays.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import Foundation
// Type inference FTW.
var strings = ["a", "b", "c"]
// Declare the type of contained elements.
var strings2: [String] = ["d", "e", "f"]
// Declare an empty array.
var strings3 = [String]()
// Fill an array.
var strings4 = [String](count: 3, repeatedValue: "hey")
// Arrays must contain values of a single type.
// Appending.
strings += ["d"]
strings.append("e")
strings += ["f", "g"]
// Joining.
strings3 = strings + strings2
// Checking length.
print(strings.count) // 7
// # Accessing elements
print(strings[0]) // a
print(strings.first!) // a
print(strings[strings.endIndex - 1]) // g
print(strings.last!) // g
// # Assigning elements
strings[0] = "a"
// # Slices
strings[0..<1] = ["a"] // Exclusive (basically the same as the prev assignment)
strings[0...1] = ["a", "b"] // Inclusive
strings[0...3] // ["a", "b", "c", "d"]
strings[0..<strings.endIndex] // ["a", "b", "c", "d", "e", "f", "g"]
// # Methods
if strings.isEmpty {
print("empty")
} else {
print("populated") // populated
}
strings.insert("a", atIndex: 0) // Insert, not replace
print(strings.removeAtIndex(0)) // a
strings.map({
(str: String) -> String in
return str + "0"
}) // ["a0", "b0", "c0", "d0", "e0", "f0", "g0"]
strings.removeLast()
// # Clearing
strings.removeAll(keepCapacity: false)
strings = []
// # Using a loop to create a multidimensional array
var rows = 10, cols = 10
var dimensional = Array<Array<Int>>()
for col in 0..<10 {
dimensional.append(Array(count: rows, repeatedValue:Int()))
}