初识Swift集合之字典集合
字典集合 字典表示一种非常复杂的集合, 允许按照某个键来访问元素 字典集合的声明与初始化: var strudentDictionary1 : Dictionary<Int,String> = [102 : " Jack",105 : "Mark",107 : "Jay"] ; //这里声明里一个strudentDictionary1 的字典集合,他的键是 Int 类型,他的值为String类型 var strudentDictionary2 =[102 : " Jack",107 : "Jay"] ; let strudentDictionary3 =[102 : " Jack",107 : "Jay"] ; //let 声明的集合值不可变 var strdentDictionary4 = Dirctionary<Int,String> (); //声明一个空的strudentDictionary 集合 字典元素的操作 增,删,改 更改元素 strudentDictionary[102] = "十元" ; 删除元素 let dismisssStrudent = strudentDictionary.removeValueForKey(102) ; //删除指定键的元素,使用这个方法删除元素,它会返回被删除集合的值。如果不要返回值strudentDictionary.removeValueForKey(102) strudentDictionary[105]=nil //这样可以直接删除元素 这里需要注意一个特殊的方法updateValue(值,forKey : 键),如果找不到相对应的键,它会增加值;如果找到这个值,它会替换这个值。这个函数也会返回被替换或者增加 的值。 let replaceStrudent = strudentDictionary.updateValue("十元",forKey : 10) ; 也可以这么写:strudentDictionary.updateValue("十元",forKey : 10) ; 字典集合的遍历,他分为键遍历、值遍历、键和值变脸 varstudentDictionary=[102:"张三",105:"张三",109:"王五"] vari=0; println("------遍历值------"); forstudentIdinstudentDictionary.keys{ i++; if(i<=2){ print("学号:(studentId),") }else{ println("学号:(studentId)") } } println("------遍历value------"); forstudentValueinstudentDictionary.values{ i--; if(i>0){ print("姓名:(studentValue),") }else{ println("姓名:(studentValue)") } } println("------遍历Idandvalue------"); for(studentId,studentValue)instudentDictionary{ i++; if(i<=2){ print("(studentId):(studentValue),") }else{ println("(studentId):(studentValue),") } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- postgresql – 在postgres上使用索引选择慢sql
- ReferenceError: Error #1065: 变量 dataGridArray 未定义
- cocos2d-js游戏设计常用函数笔记(持续更新) (2014-08-18 15
- Jackson、FastJson、Gson序列化比较,默认配置下
- c# – 如何测试AD密码是否符合配置的复杂性要求?
- ruby-on-rails – Rails服务器返回HTTP状态0
- flex中给alert中的按钮添加方法
- 使用Oracle客户端11部署.NET应用程序所需的最少安装程序是什
- Cocos2d-js 学习笔记<一> 运行Hello World
- react-native-vector-icons使用(二)