//
// ViewController2.swift
// swiftT
//
// Created by wjwdive on 2020/5/15.
// Copyright @ wjwdive All rights reserved.
//
import UIKit
class ViewController2: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//字面量 創建空數組
let array:[Int] = []
//字面量
let array1: [String] = []
//字面量
let arrNums = [1, 2, 3, 4]
print("array : ", array, "type array: ", type(of: array) ) //[] 空數組
print("array1 : ", array1, "type array1: ", type(of: array1)) //[] 空數組
print("arrNums : ", arrNums, "type of arrNums : ", type(of: arrNums))
let arrStr = [""]
print("arrStr : ", arrStr, "type of arrStr : ", type(of: arrStr))
//初始化器
var chars = Array("Jarvis")
print(chars)
let numbers = Array(1...9)
print(numbers)
//用字典的鍵初始化
let persons = ["Jarvis": 18, "Marvis": 19, "Harvis": 20]
let names = Array(persons.keys)
print(names)
//使用初始化器 創建空數組
//1、[類型]()
//2、Array<類型>()
//初始化器參數
// init(repeating repeatedValue: Element, count: Int)
// init(arrayLiteral elements: Element...)
// init<S>(_ elements: S) where S: Sequence, Self.Element == S.element
// init(from decoder: Decoder) throws
//數組的遍歷方法
// for-in 循環
// forEatch 循環 , 不能使用break, continue跳出整個循環,使用return 只能退出一次循環
// enumerated() ,同時得到 索引和值
let numberss = [1,2,3,4,5]
//enumberated() 遍歷
for (index, num) in numberss.enumerated() {
print("index ", index, " num ", num)
}
//for in遍歷
for i in 0..<numberss.count {
print("the index is \(i)")
print(numberss[i])
}
// iterator 遍歷
var it = numberss.makeIterator()
while let num = it.next() {
print(num)
}
// indeces
for i in numberss.indices {
print("the index is \(i)")
print(numberss[i])
}
// 判斷是否包含指定元素
// contains(_:) 判斷數組是否包含給定元素
// contains(where:) 判斷數組是否包含復合給定條件的元素
// allSatisfy(_:) 判斷數組的每一個元素都符合給定個的條件
let arrt = [1, 2, 34, 17, 28]
print("arrt contains 1 ",arrt.contains(1))
print("arrt contains > 10 element ",arrt.contains(where: {$0 > 10}))
//查找元素
// first 返回數組第一個元素(optional), 如果數組為空,返回nil
// last 返回數組最后一個元素(optional), 如果數組為空, 返回nil
// first(where:) 返回數組第一個符合條件的元素(optional),
// last(where:) 返回數組最后一個符合條件的元素(optional),
print("arrt first element ", arrt.first)
print("arrt last element ", arrt.last)
//查找索引
// firstIndex(of:) 返回給定的元素在數組中出現的第一個位置
// lastIndex(of:) 返回給定的元素在數組中出現的最后一個位置
// firstIndex(where:{$0 > 10}) 返回符合條件的第一個元素索引
// lastIndex(where:{$0 > 10}) 返回符合條件的最后一個元素索引
print("arrt fisrtIndex ", arrt.firstIndex(of: 2))
print("arrt lartIndex ", arrt.lastIndex(of: 2))
print("arrt firstIdex where arrt {$0 > 25}", arrt.firstIndex(of: 34))
print("arrt lastIdex where arrt {$0 > 25}", arrt.lastIndex(of: 34))
//返回最小,最大值
// min()
// max()
//安裝給定的方式比較元素並返回數組中的最小元素
// min(by:)
//利用給定的方式比較元素並返回最大元素
// max(by:)
let arrTuple = [(45, "error1"), (23, "error2"), (30, "error3")]
print(arrTuple.min {a, b in a.0 < b.0})// Optional((23, "error2"))
print(arrTuple.max {a, b in a.0 < b.0})// Optional((45, "error1"))
print(arrTuple.min(by: { (a, b) -> Bool in
a.0 < b.0
}))// Optional((23, "error2"))
//比較的是 error1,error2, error3
print(arrTuple.max(by: { (a, b) -> Bool in
a.1 < b.1
}))// Optional((30, "error3"))
//數組的添加和刪除
// 1、 append(_:) 在數組末尾添加一個元素
// 2、 append(contentsOf:) 在數組末尾添加多個元素
// 3、 insert(_: at) 在指定位置插入一個元素
// 4、 insert(contentsOf: at: ) 在指定位置插入多個元素
// i移除單個元素
//1、remove(at:) 移除並返回指定位置的一個元素
//2、removeFirst() 移除並返回數組的第一個元素
//3、popFirst() 移除並返回數組中的第一個元素,數組為空時返回nil
//4、removeLast() 移除並返回數組的最后一個元素
var numbs: [Int] = [Int](2...7)
print("numbs \n", numbs)
numbs.append(8)
print("numbs.append ", numbs)
numbs.append(contentsOf: 100...110)
print("numbs.append(contentsOf: 100...110) \n", numbs)
numbs.insert(1, at: 0)
print(" numbs.insert(1, at: 0) \n", numbs)
numbs.insert(contentsOf: 200...205, at: numbs.endIndex)
print("numbs.insert(contentsOf: 200...205, at: numbs.endIndex) \n",numbs)
numbs.remove(at: 1)
print("numbs.remove(at: 1) \n",numbs)
numbs.removeFirst()
print("removeFirst \n", numbs)
numbs.removeLast()
print("removeLast \n", numbs)
let n1 = numbs.popLast()
print("numbs.popLast() \n", n1)
//移除單個元素
//1、 removeSubrange(_:)
//2、 removeAll()
//3、 removeAll(keepingCapacity:) 移除數組所有元素,保留數組容量
numbs.removeSubrange(1...3)
print("numbs.removeSubrange(1...3) \n", numbs)
numbs.removeAll(keepingCapacity: true)
print("numbs.removeAll(keepingCapacity: true) \n", numbs)
// 字符串也是 collection, Element 是 Character類型
var charss: [Character] = ["a", "b", "c"]
charss.insert(contentsOf: "hello", at: 0)
print(charss)
var chs: [Character] = ["a", "b", "c"]
let removedChar = chs.remove(at: 1)
print(removedChar)
print(chs)
var chs1: [Character] = ["a", "b", "c"]
let removedChar1 = chs1.removeFirst()
print(removedChar1)
print(chs1)
//數組切片
//1、 ArraySlice 是數組或者其他ArraySlice 的一段連續切片,和原數組共享內存
//2、 當要改變 ArraySlice 的時候, ArraySlice 會copy出來,形成單獨內存
//3、 ArraySlice 擁有和 Array 基本類似的方法
// 通過 Drop 得到ArraySlice
// dropFirst(:) 移除元數組前面指定個數的元素,得到一個ArraySlice
// dropLast(:) 移除原數組后面指定個數的元素得到一個 ArraySlice
// drop(:) 移除原數組符合指定條件的元素,得到一個 ArraySlice
let arrayS = [1, 2, 3, 4, 5, 6]
print("待切片原數組: arrayS: \n ", arrayS)//[1, 2, 3, 4, 5, 6]
let slice1 = arrayS.dropFirst()
print("arrayS.dropFirst(): \n", slice1)//[2, 3, 4, 5, 6]
let slice2 = arrayS.prefix(3)
print("arrayS.prefix(3): \n",slice2)// [1, 2, 3]
let slicew = arrayS.prefix(while:{
$0 < 2
})
print(" arrayS.prefix(while : \n", slicew)// arrayS.prefix(while : [1]
//通過prefix 得到ArraySlice
//1、 prefix() 獲取數組前面指定個數的元素組成的 ArraySlice
//2、 prefix(upTo:) 獲取數組到指定位置,(不包含指定位置)前面的元素組成的ArraySlcie
//3、 prefix(through:) 獲取數組到指定位置(包含指定位置)前面的元素組成的 ArraySlice
//4、 prefix(while:) 獲取數組前面符合條件的元素,(到第一個不符合條件的元素截止) 組成的 ArraySlice
//通過suffix得到ArraySlice
//suffix() 獲取數組后面指定個數的元素組成的ArraySlice
//suffix(from:) 獲取數組從指定位置到結尾(包含指定位置)的元素組成的ArraySlice
//通過range 得到ArraySlice
//可以通過對數組下標指定Range 獲取 ArraySlice, 可以使用 閉區間,半開半閉區間,單側區間,... 得到整個數組的ArraySlice
//ArraySlice 轉換為 Array
//ArraySlice 無法直接賦值給一個Array的常量或者變量,需要使用Array(slice)
//ArraySlcie 和 原Array 是相互獨立的
// ArraySlcie 和 原Array 是相互獨立的,他們添加刪除元素不會影響對方
}
}