Swift学习笔记(5)--数组
数组的下标从0开始计数,相关方法属性涉及到下标时也从0开始计数
1.定义:
//1.可变数组
var cityArray = ["Portland","San Francisco","Cupertino"]
//2.不可变数组
let cityArray2 = ["Portland","San Francisco","Beijing"]
//3.空数组
var animalArray = String[]()
var animalAgeArray = Int[]()
2.基本方法:统计总数,判断非空
//1.统计总数
var cityArray: String[] = ["Portland","San Francisco","Cupertino"]
let count = cityArray.count //2.判断非空
if cityArray.isEmpty {
println("Empty")
}else{
println("Not Empty")
}
3.遍历
var animalArray: String[] = ["Dog", "Cat", "Fish", "Owl" ] //1.元素遍历
for animal in animalArray {
println(animal)
} //2.带下标遍历
for(index,animal) in enumerate(animalArray) { println("The \(animal) is at index:\(index)") } for var i = 0; i < animalArray.count; i++ {
println(animalArray[i])
} for i in 0..animalArray.count {
println(animalArray[i])
}
4.添加元素或者子数组
//1.添加单个元素
var cityArray: String[] = ["Portland","San Francisco","Cupertino"]
cityArray.append("Seattle") //函数添加
cityArray += "Seattle" //运算符重载添加 //2.添加子数组
var cityArrayA: String[] = ["Portland","San Francisco","Cupertino","Seattle"]
var cityArrayB: String[] = ["Vancouver", "Los Angeles", "Eugene"] cityArrayA += ["Vancouver", "Los Angeles", "Eugene"] //固定子数组
cityArrayA += cityArrayB; //变量子数组
5.改变数组元素
注:改变多个元素,如果下标与元素的数量不一致,以下标做多退少补
var cityArray: String[] = ["Portland","San Francisco","Cupertino","Seattle","NewYork"] //1.改变单个元素
cityArray[0] = "Oregon" //2.改变多个元素,如果下标与元素的数量不一致,以下标做多退少补
//2.1 相等时
var cityArray0 = cityArray
cityArray0[0...2] = ["Beijing","Shanghai","Guangzhou"]
println(cityArray0) //[Beijing, Shanghai, Guangzhou, Seattle, NewYork]
//2.2 下标多于元素时,原数组中多出的下标数据被remove出原数组,即多退
var cityArray2 = cityArray
cityArray2[0...3] = ["Beijing","Shanghai","Guangzhou"]
println(cityArray2) //[Beijing, Shanghai, Guangzhou, NewYork]
//2.3 下标少于元素时,将多出的元素添加到原数组,即少补
var cityArray3 = cityArray
cityArray3[0...1] = ["Beijing","Shanghai","Guangzhou"]
println(cityArray3) //[Beijing, Shanghai, Guangzhou, Cupertino, Seattle, NewYork]
6.删除元素
//1.删除指定元素
var animalArray: String[] = ["Dog", "Cat", "Fish", "Owl", "Beaver"]
animalArray.removeAtIndex(4)
println(animalArray) //[Dog, Cat, Fish, Owl] //2.删除最后一个元素
var animalArray2: String[] = ["Dog", "Cat", "Fish", "Owl" ]
animalArray2.removeLast() //[Dog, Cat, Fish]
println(animalArray2) //3.删除所有元素
var animalArray3: String[] = ["Dog", "Cat", "Fish", "Owl" ]
animalArray3.removeAll()
println(animalArray3) //[]
注:如果你想保留原来被删除的元素,可以使用let item = array.removeFunction() 来保留
var animalArray: String[] = ["Dog", "Cat", "Fish", "Owl", "Beaver"]
let item = animalArray.removeAtIndex(4)
println(item) //Beaver
7.排序
基本排序
var animalArray: String[] = ["Dog", "Cat", "Fish", "Owl" ]
sort(animalArray)
//The array values are now [Cat, Dog, Fish, Owl]
反向重排
var animalArray: String[] = ["Dog", "Cat", "Fish", "Owl" ]
animalArray.reverse()
//The array values are now ["Owl", "Fish", "Cat", "Dog"]
自定义快速排序
func quicksort_swift(inout a:Int[], start:Int, end:Int) {
if (end - start < 2){
return
}
var p = a[start + (end - start)/2]
var l = start
var r = end - 1
while (l <= r){
if (a[l] < p){
l += 1
continue
}
if (a[r] > p){
r -= 1
continue
}
var t = a[l]
a[l] = a[r]
a[r] = t
l += 1
r -= 1
}
quicksort_swift(&a, start, r + 1)
quicksort_swift(&a, r + 1, end)
} var a_swift:Int[] = [0,5,2,8,1234,-1,2]
let swift_start:UInt64 = mach_absolute_time();
println(swift_start)
quicksort_swift(&a_swift, 0, a_swift.count)
let swift_stop:UInt64 = mach_absolute_time();
println(swift_stop)
println(a_swift)
Swift学习笔记(5)--数组的更多相关文章
- Swift学习笔记(12)--数组和字典的复制
Swift中,数组Array和字典Dictionary是用结构来实现的,但是数组与字典和其它结构在进行赋值或者作为参数传递给函数的时候有一些不同. 并且数组和字典的这些操作,又与Foundation中 ...
- swift学习笔记之---数组、字典、枚举、结构体
1.数组-Array let types = ["none","warning","error"] //省略类型的数组声明 let menb ...
- 【swift学习笔记】二.页面转跳数据回传
上一篇我们介绍了页面转跳:[swift学习笔记]一.页面转跳的条件判断和传值 这一篇说一下如何把数据回传回父页面,如下图所示,这个例子很简单,只是把传过去的数据加上了"回传"两个字 ...
- Swift学习笔记(一)搭配环境以及代码运行成功
原文:Swift学习笔记(一)搭配环境以及代码运行成功 1.Swift是啥? 百度去!度娘告诉你它是苹果最新推出的编程语言,比c,c++,objc要高效简单.能够开发ios,mac相关的app哦!是苹 ...
- PHP学习笔记之数组篇
摘要:其实PHP中的数组和JavaScript中的数组很相似,就是一系列键值对的集合.... 转载请注明来源:PHP学习笔记之数组篇 一.如何定义数组:在PHP中创建数组主要有两种方式,下面就让我 ...
- JavaScript学习笔记之数组(二)
JavaScript学习笔记之数组(二) 1.['1','2','3'].map(parseInt) 输出什么,为什么? ['1','2','3'].map(parseInt)//[1,NaN,NaN ...
- swift学习笔记4——扩展、协议
之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...
- swift学习笔记3——类、结构体、枚举
之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...
- swift学习笔记2——函数、闭包
之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...
随机推荐
- cocos2d_x_01_环境搭建
终于效果图: Cocos2d-x-3.3 Mac 安装 下载地址: 參考文档: 在线API列表: Cocos2d-x-3.3 版本号 从配置安装到创建项目都是命令行 官网下载最新版本号Cocos2 ...
- Import Example Dataset
Overview The examples in this guide use the restaurants collection in the test database. The followi ...
- android常用控件的使用方法
引言 xml很强大 TextView <TextView android:id="@+id/text_view" android:layout_width="mat ...
- jzoj3454 表白(love)解题报告(01分数规划+DP)
题目链接:https://jzoj.net/senior/#contest/show/2414/2 题目描述: 鸡腿是CZYZ的著名DS,但是不想追妹子的DS不是好GFS,所以鸡腿想通过表白来达到他追 ...
- 安卓开发--AsyncTask
package com.cnn.asynctask; import android.app.Activity; import android.content.Intent; import androi ...
- HD-ACM算法专攻系列(1)——第几天?
题目描述: 源码: #include <cstdio> #include <ctime> int main() { int year, month, day; int sum; ...
- SharePoint 2010 开发人员学习指南
kaneboy 翻译,一切内容版权归 Microsoft.1. SharePoint 开发起步教程 这是一个为准备进入到 SharePoint 开发领域的 .NET 开发人员所准备的免费在线学习课程. ...
- zookeeper启动闪退
编辑zkServer.cmd在它的尾行加上 pause 就可以将闪退停住来观察闪退的原因. 遇到Exception in thread "main" java.lang. ...
- 安装Debugging Tools时出现错误Setup could not find the file WinSDK_amd64的处理
安装Debugging Tools时出现错误Setup could not find the file WinSDK_amd64的处理 1.软件来源: 微软官网下载SDK ISO安装包(含有debu ...
- 使用WinNTSetup安装win10时提示efi part有红叉(win10安装UEFI系统安装)
1.装载ImDisk虚拟磁盘 2.格式化硬盘 *格式化时注意”创建新ESP分区 3.使用 WinNTSetup 选择win10安装程序 *1. “BOOTMGR PBR "后有感叹号不用管, ...