//
// main.swift
// S2 ArrayLearn
// Created by ivan on 15/10/22.
// copyright © 2015年 bingxu. All rights reserved.
//
import Foundation
//数组的用法
var shopPingList:Array = ["白菜","香蕉",26)">挂面",26)">洗发水"]//定义一个数组
print("这个shopPingList里头有\(shopPingList.count)个元素")//计算这个数组里的元素个数
if shopPingList.isEmpty{ //用isEmpty方法判断一个数组是否为空数组
print("shopPingList数组是一个空数组")
}else {
print("shopPingList数组不是一个空数组")
}
//遍历这个数组并打印出来
for item inshopPingList{
print(item)
}
shopPingList.append("牙刷")//在数组里头添加一个元素,这种方式默认在数组末尾添加元素
shopPingList.insert("牙膏",atIndex: 2) //在制定位置添加一个元素
//打印所有元素
//查看数组里头的元素个数
print("现在shopPingList数组里头\(shopPingList.count)个元素")
//可以将数组里的元素单独赋值并且调用
var firstItemOfshopPingList =shopPingList[0] //将shopPingList里头的第一个元素赋值给变量firstItemOfshopPingList
print(firstItemOfshopPingList) //将shopPingList的第一个元素打印出来
//将shopPingList的第一个元素换成“青菜”
shopPingList[0] = "青菜"
print(shopPingList[0]) //重新打印修改后的shopPingList[0]
print("\n")
//批量更改数组里头的元素
//将shopPingList里第2、3、4个元素更改为“橘子”,“黄瓜”,“牛肉”
shopPingList[1...3] = ["橘子",26)">黄瓜",26)">牛肉"] //注意这里是3个点号
//将更改后的shopPingList打印出来
for item inshopPingList{
print(item)
}
print("\n")//回车
//删除item
//删除第一个item
shopPingList.removeAtIndex(0)//删除了第一个item后原来的第2个item就变成新数组的第一个item
shopPingList.removeFirst() //也是删除第一个元素,同上一句语句
//重新打印清单
print("\n")
//删除最后一个item
shopPingList.removeLast()
print("\n")
//Demo 2
var threeFruit =Array(count: 3,repeatedValue: "水果")//定义了一个包含了三个元素的数组,其中三个元素一模一样
for itemin threeFruit{ //将数组里头里的元素全部打印出来
print(item)
}
print("\n")
var threevegetable =Array(count: 3,26)">蔬菜")//定义一个包含三个“蔬菜”的数组
for item inthreevegetable{
print(item)
}
//将两个数组直接加起来
var vegetableAddFruit =threevegetable + threeFruit
for itemin vegetableAddFruit{ //将add后的数组打印出来
print(item)
}
print("vegetableAddFruit数组里头一共有\(vegetableAddFruit.count)个元素")//将数组里头的元素个数打印出来
总结以上是内存溢出为你收集整理的swift 学习笔记 --> Array全部内容,希望文章能够帮你解决swift 学习笔记 --> Array所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)