容器总结:
- OC中为我们提供了三大容器,数组,字典和集合,三个容器中存储的都是对象类型。
- 数组:是一个有序的集合,当我们需要管理有序的⼀组对象的时候需要使⽤用数组。
- 字典:是一个无序集合,里面存储的对象是一个一个的键值对,我们获取数据的时候需要使用对应的key来操作,
并且key在字典中是唯一的。 - 集合:集合是一个无序的容器,并且容器中的对象不能够重复。
- 可变容器类对象是不可变容器类对象的子类,在拥有父类功能的基础上,扩充了对原有对象的增删改操作。
// _ooOoo_
// o8888888o
// 88" . "88
// (| -_- |)
// O\ = /O
// ____/`---'\____
// . ' \\| |// `.
// / \\||| : |||// \
// / _||||| -:- |||||- \
// | | \\\ - /// | |
// | \_| ''\---/'' | |
// \ .-\__ `-` ___/-. /
// ___`. .' /--.--\ `. . __
// ."" '< `.___\_<|>_/___.' >'"".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `-. \_ __\ /__ _/ .-` / /
// ======`-.____`-.___\_____/___.-`____.-'======
// `=---='
//
// .............................................
// 佛祖保佑 永无BUG
// 佛曰:
// 写字楼里写字间,写字间里程序员;
// 程序人员写程序,又拿程序换酒钱。
// 酒醒只在网上坐,酒醉还来网下眠;
// 酒醉酒醒日复日,网上网下年复年。
// 但愿老死电脑间,不愿鞠躬老板前;
// 奔驰宝马贵者趣,公交自行程序员。
// 别人笑我忒疯癫,我笑自己命太贱;
// 不见满街漂亮妹,哪个归得程序员?
数组(NSArray)及可变数组(NSMutableArray)
初始化相关
//初始化(object:对象) initWithObject注意有无s(单个,多个赋值)
NSArray *names = [[NSArray alloc]initWithObjects:@"科比",@"加内特",@"麦迪",@"杜兰特",nil];
//复制初始化
NSArray * namecopy = [NSArray arrayWithArray:names];
NSLog(@"%@",namecopy);
便利构造器
//便利构造器
NSArray *name = [NSArray arrayWithObjects:@"科比",@"加内特",@"麦迪",@"杜兰特", nil];
字面量
//字面量,语法糖
NSArray *namename = @[@"科比",@"加内特",@"麦迪",@"杜兰特"];
数组数量
//数组数量(下标)
NSLog(@"%ld",[name count]);
读取某数组
//读取下标为3的数组(Index:指针,索引)
NSLog(@"%@",[namename objectAtIndex:3]);
NSLog(@"%@",namename[2]);
判断是否包含
//判断是否包含"科比" (返回BOOL)
//- (BOOL)containsObject:(id)anObject;
if ([names containsObject:@"科比"]) {
NSLog(@"YES");
} //(contains:包含,容纳)
获取数组某元素对应的索引
//获取数组元素对应的索引 (Index:指针,索引)
//- (NSUInteger)indexOfObject:(id)anObject;
NSUInteger index = [name indexOfObject:@"杜兰特"];
NSLog(@"%lu",index);
NSLog(@"%ld",[name indexOfObject:@"科比"]);
截取字符串分段放入数组 分割拼接
//按照字符串进⾏截取,将截取的多段字符串放⼊数组中
//- (NSArray *)componentsSeparatedByString:(NSString *)separator;
//(component:构成);(Separat:分割,分开)
NSString *str = @"Imshy.memeda.piapiaia";
//通过自定义字符"."分割字符串成数组
NSArray *cut = [str componentsSeparatedByString:@"."];
NSLog(@"%@",cut);
//通过自定义字符将数组中的元素"&" 拼接字符串(join:加入)
NSString *string = [cut componentsJoinedByString:@"&"];
NSLog(@"%@",string);
练习一 – 分割拼接
//存在如下的字符串,将其中的图⽚的网址提取出来。http://www.imanhua.com/Cover/2011-10/http.jpg&http://www.imanhua.com/Cover/2011-09/op.jpg&http://www.imanhua.com/Cover/2012-04/yjdwb.jpg
//练习test
//通过"&"分割
NSString *str1 = @"http://www.imanhua.com/Cover/2011-10/http.jpg&http://www.imanhua.com/Cover/2011-09/op.jpg&http://www.imanhua.com/Cover/2012-04/yjdwb.jpg";
NSArray *arr1 = [str1 componentsSeparatedByString:@"&"];
NSLog(@"%@",arr1);
//通过String拼接
NSString *strjoin = @" " ;
for (NSInteger i = 0 ; i < [arr1 count]; i++) {
strjoin = [strjoin stringByAppendingString:arr1[i]];
}
NSLog(@"%@",strjoin);
可变数组初始化相关
//可变数组 初始化
NSMutableArray *name1 = [[NSMutableArray alloc]initWithCapacity:0];
//便利构造器
NSMutableArray *name2 = [NSMutableArray arrayWithCapacity:0];
//字面量
NSMutableArray *name3 = [@[@"frank", @"duck",
@"monkey", @"cow"] mutableCopy];
可变数组增删改
//添加对象
[name1 addObject:@"加内特"];
//最后插入数组(放入数组) (add:添加) (from:从;to:到)
[name3 arrayByAddingObjectsFromArray:name1];
//在下标1处插入对象(insert:插入)
[name3 insertObject:@"KG" atIndex:1];
NSLog(@"%@",name3);
//移除对象KG(remove:移除)
[name3 removeObject:@"KG"];
//移除数组最后一个对象
[name3 removeLastObject];
//移除所有对象
[name2 removeAllObjects];
//数组中移除指定位置
[name3 removeObjectAtIndex:1];
NSLog(@"%@",name3);
//使⽤用指定的对象替换指定位置的对象 (replace:替换)
[name3 replaceObjectAtIndex:1 withObject:@"IMCOOL"];
NSLog(@"%@",name3);
//交换指定的两个下标对应的对象 (exchange:交换)
[name3 exchangeObjectAtIndex:0 withObjectAtIndex:1];
NSLog(@"%@",name3);
练习二 – OC版冒泡排序
//方法1
NSMutableArray *p = [[NSMutableArray alloc]initWithObjects:@"3",@"2",@"1",@"0", nil];
NSInteger sum = [p count];//获取p的元素数量
for (int i = 0; i < sum - 1; i++) {
for (int j = 0; j < sum - i - 1; j++) { //双重循环
int a = [[p objectAtIndex:j] intValue];//从对象获取数值
int b = [[p objectAtIndex:j+1] intValue];
if (a < b) {//才能对比
[p replaceObjectAtIndex:j withObject:[NSString stringWithFormat:@"%d",b]];//实例方法作交换
[p replaceObjectAtIndex:j + 1 withObject:[NSString stringWithFormat:@"%d",a]];
}
}
}
NSLog(@"%@",p);
//方法2
NSMutableArray *p1 = [[NSMutableArray alloc]initWithObjects:@"3",@"2",@"1",@"0", nil];
NSInteger sum1 = [p count];
NSString * max;
for (int i = 0; i < sum1 - 1; i++) {
for (int j = 0; j < sum1 - i - 1; j++) {
int a = [p1[j] intValue];//获取数值
int b = [p1[j+1] intValue];
if (a < b) {//C语言冒泡排序
max = p1[j];
p1[j] = p1[j+1];
p1[j+1] = max;
}
}
}
NSLog(@"%@",p1);
//方法3(最简法相对)
NSNumber *number;
NSMutableArray *array = [NSMutableArray array];
for (int i = 0; i < 20; i++) {
number = [ NSNumber numberWithInteger:arc4random() % 101];
[array addObject:number];//随机数放入数组
}
for (int i = 0;i < [array count] - 1 ; i++) {
for (int j = 0; j < [array count] - 1 - i; j++) {
if ([array [j] integerValue] > [array[j+1] integerValue]) {//从对象中获取数值后对比
[array exchangeObjectAtIndex:j withObjectAtIndex:(j+1)];//可变数组实例交换方法,j指的元素与j+1指的元素交换
}
}
}
NSLog(@"%@",array);
//数组里放的都是对象(NSString之类)
练习题三 – 简单增删改
//练习题
NSMutableArray *SHU = [@[@"cool",@"memeda",@"yoyoy",@"papapa",@"haha"]mutableCopy];
//添加三个名字
[SHU addObject:@"KG"];
[SHU addObject:@"AI"];
[SHU addObject:@"T-Mac"];
//查找指定名字,并用另一名字替换
if ([SHU containsObject:@"T-Mac"]) {
[SHU replaceObjectAtIndex:[SHU indexOfObject:@"T-Mac"]withObject:@"memeda"];
NSLog(@"%@",SHU);
}
//移除"AI" (删除)
[SHU removeObject:@"AI"];
字典(NSDictionary)与可变字典(NSMutableDictionary)
初始化相关
NSDictionary * dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"阿康", @"213",@"蔡伦",@"name",@"郑和",@"name1",@"魏忠贤",@"name2",nil];
便利构造器
NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"阿康",@"key",@"华佗",@"key2",@"华雄",@"key3",nil];
字面量
NSDictionary *dic1 = @{@"key1":@"阿康",@"key2":@"华佗"};
获取所有Key和Value
//获得所有key
NSArray *keyarray = [dic allKeys];
NSLog(@"%@",keyarray);
//获得所有value
NSArray *valuearray = [dic1 allValues];
字典中取值(元素)
//取"蔡伦"值 (字典只能通过key取值)
NSLog(@"%@",[dic objectForKey:@"name"]);
练习四 – 输出格式 [Key]:[value]
//练习 输出格式 [Key]:[value]
NSDictionary *testDic = [NSDictionary dictionaryWithObjectsAndKeys:
@"盖伦",@"德玛西亚之力",
@"嘉文",@"德玛西亚皇子",
@"赵信",@"德邦总管",
@"拉克丝",@"光辉女郎",
@"薇恩",@"暗夜猎手",
@"奎因",@"德玛西亚之翼",
@"泰隆",@"刀锋之影",
@"德莱文",@"荣耀行刑官",
@"德莱厄斯",@"诺克萨斯之手",
@"卡特琳娜",@"不祥之刃",
@"凯南",@"狂暴之心",
@"提莫",@"迅捷斥候",
@"兰博",@"机械公敌",
@"塔姆",@"河流之王",
@"乐芙兰",@"诡术妖姬",
@"莫甘娜",@"堕落天使",
@"亚索",@"疾风之刃", nil];
NSArray *lolkey = [testDic allKeys];
for (int i = 0; i < [testDic count]; i++) {
NSLog(@"[%@]:[%@]", lolkey[i],[testDic objectForKey:[lolkey objectAtIndex:i]]);
}
可变字典(NSMutableDictionary)初始化相关
//NSMutableDictionary
NSMutableDictionary *test = [NSMutableDictionary dictionary];
//可添加或修改
[test setObject:@"加内特" forKey:@"KG"];
移除
//移除指定的键对应的键值对
[test removeObjectForKey:@"KG"];
//移除所有键值对
[test removeAllObjects];
集合(NSSet)
初始化相关
集合经常用来处理重⽤问题(Key唯一)。
//集合(互异性,无序性) 哈希算法(效率极高)
NSSet *set = [[NSSet alloc]initWithObjects:@"加内特",@"诺维斯基",@"邓肯", nil];
//便利构造器(区分带s)
NSSet *sett = [NSSet setWithObjects:@"加内特",@"诺维斯基",@"邓肯",@"阿尔德里奇",@"加索尔",nil];
注意:不可变集合一旦创建,集合中的对象⽆法修改,只能从集合中读取对象,并且没有快速创建集合对象的字面量。
取值和包含关系
//从集合中取出一个对象(随机且并没有什么卵用)
[sett anyObject];
//判断集合中是否包含指定对象
[set containsObject:@"加内特"]
可变集合(NSMutableSet)初始化相关
//NSMutableSet初始化
NSMutableSet *muset = [[NSMutableSet alloc]initWithCapacity:0];
//便利构造器
NSMutableSet *mutset = [NSMutableSet setWithObjects:0];
可变集合增删
//添加对象
[muset addObject:@"KG"];
//移除一个对象
[muset removeObject:@"KG"];
//移除所有对象
[mutset removeAllObjects];
Ps : 继续努力