标签:swift 常量 返回值 ... 数据集 min() move swift开发 ica
//
// ViewController.swift
// Swift+Array
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.yellow;
//数组初始化
var numbers = [1,2,3,4,5,6];
print(numbers)
var vowels = ["a","b","c","d"];
print(vowels)
let emptyArray = [Int]()
//数组长度
let num = vowels.count;
print(num)
//判空
print(numbers.isEmpty)
print(emptyArray.isEmpty)
//获取元素
print(vowels[2])//注意不要犯数组越界的错误
//获取第一个元素和最后一个元素,返回的是可选型
let first_vowels = vowels.first;
print(first_vowels)
let last_vowels = vowels.last;
print(last_vowels)
//获取最小值,最大值
let min_numbers = numbers.min();
print(min_numbers)
let max_vowels = vowels.max();
print(max_vowels)
//使用范围
let numbersss = numbers[2..<4];
print(numbersss)
let vumberss = numbers[2..<numbers.count]
print(vumberss)
//包含
vowels.contains("a")
vowels.contains("b")
let letter = "a";
if vowels.contains(letter) {
print("\(letter) in vowel")
}else{
print("\(letter) not in vowel")
}
//获取索引,返回值为可选型
if let index = vowels.index(of: "c") {
print("c ise a vowel in position \(index + 1)")
}else{
print("c is not a vowel")
}
//遍历
for index in 0..<numbers.count {
print(numbers[index])
}
for number in numbers {
print(number)
}
for (index, vowel) in vowels.enumerated() {
//遍历数组所以和元素
print("\(index) + 1 : \(vowel))")
}
//比较
let oneToFive = [1,2,3,4,5,6];
if oneToFive == numbers {
print(true)
}
//swift 3.0之前数组是有序的数据集合,swift 3.0后为无序
var courses = ["a","b","c"]
//添加元素
courses.append("d");
print(courses)
//数组常量.使用let定义的数组不可以更改任何内容
courses += ["e"]
print(courses)
courses += ["F"]
print(courses)
//+=后面必须和前面的类型一直
//两个数组相加
courses = courses + ["H","I"];
print(courses)
//插入
courses.insert("W", at: 1)
print(courses)
//删除
courses.removeLast()
print(courses)
courses.removeFirst()
print(courses)
courses.remove(at: 1)
print(courses)
//区间删除
courses.removeSubrange(0..<2)
print(courses)
//修改元素
courses[0] = "Z"
print(courses)
//范围修改
courses[1...3] = ["Q","W","E"];
print(courses)
courses[0...3] = ["W"]
print(courses)
//二维数组
var board = [ [1024,16,2,0] , [256,4,2,0] , [64,2,0,0] , [2,0,0,0] ]
//var board:[[Int]] = [ [1024,16,2,0] , [256,4,2,0] , [64,2,0,0] , [2,0,0,0] ]
//var board:[Array<Int>] = = [ [1024,16,2,0] , [256,4,2,0] , [64,2,0,0] , [2,0,0,0] ]
//var board:Array<[Int]> = [ [1024,16,2,0] , [256,4,2,0] , [64,2,0,0] , [2,0,0,0] ]
//var board:Array<Array<Int>> = [ [1024,16,2,0] , [256,4,2,0] , [64,2,0,0] , [2,0,0,0] ]
// 二维数组获取元素
// board[0]
// board[0][0]
print(board[0])
print(board[0][0])
// 获取二维数组两个维度的信息
board.count
board[0].count
// Swift中的二维数组,每一维度的元素数目可以不同
board[0].append(0)
print(board)
// 为二维数组的第一个维度添加的元素是一个数组
board.append([0,0,0,0])
print(board)
board += [ [0,0,0,0] ]
print(board)
//最后一句话 NSArray是一个类 Array是一个结构体
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
标签:swift 常量 返回值 ... 数据集 min() move swift开发 ica
原文地址:http://www.cnblogs.com/freeleader/p/7452864.html