美文网首页
(七) [Swift]Swift中的有序集合

(七) [Swift]Swift中的有序集合

作者: 修行猿 | 来源:发表于2016-08-03 20:08 被阅读91次

1.Array的初始化

var array1 :Array<Int> = Array<Int>()
var array2 :[Int] = Array<Int>()
var array3 = Array<Int>()
var threeInts = [Int](count:6,repeatedValue:1)

2.Array的长度与判空

threeInts.count
threeInts.isEmpty
threeInts.count == 0

3.Array的索引

//数组的索引取值
threeInts[1]

//数组的索引可以是一个范围
threeInts[1...2] //[1, 1]
threeInts[1..<3] //[1, 1]

//可以对数组的一个范围整体赋值
threeInts    //
threeInts[1...3] = [1,2,3]
threeInts //
threeInts[2...3] = [4] //当个数不足,会将没有值的删除
threeInts //

4.Array添加与删除元素


//添加一个元素
threeInts.append(1)
//添加一堆元素
threeInts.appendContentsOf([3,3,3,3,3])
threeInts
//使用运算符来添加一个或多个元素
threeInts+=[4,4,4,4]
//在指定位置添加一个元素
threeInts.insert(5, atIndex: 1)
//移除第一个元素
threeInts.removeFirst()

5.Array的遍历


//遍历所有的值
for number in threeInts{
    print(number)
}
//遍历索引和值
for (index,number) in threeInts.enumerate(){
    print("index:\(index) number:\(number)")
}

相关文章

网友评论

      本文标题:(七) [Swift]Swift中的有序集合

      本文链接:https://www.haomeiwen.com/subject/vgtcsttx.html