码迷,mamicode.com
首页 > 编程语言 > 详细

Swift- 数组

时间:2016-11-03 16:18:04      阅读:330      评论:0      收藏:0      [点我收藏+]

标签:dex   pen   append   有序   二维   where   ros   泛型   range   

//: 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- 数组

标签:dex   pen   append   有序   二维   where   ros   泛型   range   

原文地址:http://www.cnblogs.com/zrr-notes/p/6026823.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!