首页 > 代码库 > Swift- 数组

Swift- 数组

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #4dbf56 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #ffffff; min-height: 15.0px } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #ffffff } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #e44347 } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px "PingFang SC"; color: #4dbf56 } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #93c96a } p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #00afca } span.s1 { } span.s2 { color: #c2349b } span.s3 { color: #ffffff } span.s4 { font: 13.0px Menlo } span.s5 { font: 13.0px "PingFang SC" } span.s6 { color: #93c96a } span.s7 { color: #00afca } span.s8 { color: #8b84cf } span.s9 { color: #4dbf56 } span.s10 { font: 13.0px "PingFang SC"; color: #4dbf56 } span.s11 { font: 13.0px Menlo; color: #93c96a } span.s12 { font: 13.0px Menlo; color: #ffffff } span.s13 { font: 13.0px Menlo; color: #8b84cf } span.s14 { color: #e44347 }</style>

//: Playground - noun: a place where people can play

 

import UIKit

 

var str = "Hello, playground"

 

//容器类

 

////1.数组 Array

//

////一般形式

//var numbers:[Int] = [0,1,2,3,4,5]

//var vowels:[String] = ["A","E","I","O","U"]

//

////泛型形式

//var numbers1:Array<Int> = [0,1,2,3,4,5]

//var vowels1:Array<String> = ["A","E","I","O","U"]

//

////初始化空数组

//var emptyArray1:[Int] = []

//var emptyArray2:Array<Int> = []

//var emptyArray3 = [Int]()

//var emptyArray4 = Array<Int>()

//

////定义固定大小的数组并初始化为0

//var allZeros = [Int](repeating: 0, count: 5)

//var allZeros2 = Array<Int>(repeating: 1, count: 5)

 

 

////数组的一些属性

//var numbers = [1,2,3,4,5]

//var vowels = ["A","E","I","O","U"]

//var emptyArray = [Int]()

//

//numbers.isEmpty

//emptyArray.isEmpty

//

////数组索引

//vowels[2]

//vowels.count

//vowels.first

//vowels.last

//emptyArray.first

//emptyArray.last

//

//if let firstVowel = vowels.first {

//    print("The first vowel is" + firstVowel)

//}

//

//vowels.first!

//vowels[vowels.count - 1]

//

//numbers.min()

//numbers.max()

//

//numbers[2..<4]

//numbers[2..<numbers.count]

//vowels.contains("A")

//

//vowels.index(of: "E")

//vowels.index(of: "F")

//

////数组遍历

//for number in numbers {

//    print(number)

//}

//

//for (i, vowel) in vowels.enumerated() {

//    print("\(i + 1): \(vowel)")

//}

//

//var oneToFive = [1,2,3,4,5]

//numbers == oneToFive//数组是有序的数据集合

 

//数组的操作

 

//1.

var courses = ["玩儿转Swift","玩儿转Swift第一季","玩儿转Swift第二季"]

 

courses.append("玩儿转Swift第三季")

print(courses)

 

courses += ["玩儿转Swift第四季"]

courses = courses + ["玩儿转Swift面向协议编程","玩儿转Swift面向函数编程"]

 

print(courses)

 

 

courses.insert("玩儿转Swift设计模式", at: 5)//5是索引值,从1开始

 

courses.remove(at: 4)

 

//

//courses.removeSubrange(0..<4)

//print(courses)

 

//

courses[0..<4] = ["玩儿转Swift3"]

print(courses)

 

//二维数组

 

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]

 

board.count

board[0].count

 

board[0].append(0)

board

board.append([0,0,0,0])

board += [[2,2,2,2]]//二维数组是添加一个元素(二维数组的元素还是数组)

 

 

//NSArray

//var array1 = []

var array2 = [1,2,3,4,5] as NSArray

var array3:NSArray = [1,"Hello",3.0]

var array4:[NSObject] = [1 as NSObject,"Hello" as NSObject,3.0 as NSObject]

 

 

 

 

 

 

 

 

 

Swift- 数组