From f8abbfdf414badf5dfe43f65ad3fda4f0e8919eb Mon Sep 17 00:00:00 2001 From: H3r0Complex <9460076+H3r0Complex@users.noreply.github.com> Date: Sun, 23 Jun 2019 18:29:48 -0700 Subject: [PATCH] Added information about arrays (#32394) * Added information about arrays * added info about swift arrays * added info about swift arrays --- guide/english/swift/arrays/index.md | 64 +++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 guide/english/swift/arrays/index.md diff --git a/guide/english/swift/arrays/index.md b/guide/english/swift/arrays/index.md new file mode 100644 index 0000000000..7f1bc78893 --- /dev/null +++ b/guide/english/swift/arrays/index.md @@ -0,0 +1,64 @@ +--- +title: Arrays +--- + +# Arrays + +Arrays are a collection type that stores elements of the **same type** in an **ordered list**. + +### Initialization + +```swift +//create immutable array. +let oddNumbers: [Int] = [1,3,5,7,9]; +let evenNumbers = [2,4,6,8]; + +//create an empty mutable array +var numbers = [Int](); +``` + +### Modification +Add a single element to an array +```swift +let someNumber = 0 +numbers.append(someNumber) +``` + +Add multiple elements to an array +```swift +numbers += oddNumbers +numbers.append(contentsOf: evenNumbers) +``` + +Remove and Insert at Index +```swift +//remove element from an array at index +let removedInt = numbers.remove(at: 4) +//insert element at specific index +numbers.insert(removedInt, at: 4) +``` + +### Iteration + +You can iterate over a set using a for-in loop. Below are two different ways to do essentially the same thing. + +```swift +var showNumbers = "" +numbers = numbers.sorted() //sort the numbers in ascending order +for i in 0..