一.不可变数组
//创建空数组
NSArray *arr = [[NSArray alloc]init];
//对象方法创建数组 以nil结束
NSArray *arr1= [NSArray alloc]initWithObjects:@"sun",@"is",@"girl", nil];
//用已有数组创建新数组
NSArray *arr2 = [[NSArray array]initWithArray:arr];
//通过类方法创建数组
NSArray *arr3 = [NSArray arrayWithArray:arr2];
//类方法创建空数组
NSArray *arr4 = [NSArray array];
1.不可变数组的方法
//取一个元素
NSArray *arr = @[@"sun",@"is",@"a",@"smilling",@"girl"];
NSString *string = [arr objectAtIndex:2];
//取第一个元素
NSString *first = [arr firstObject];
//取最后一个元素
NSString *last = [arr lastObject];
//简单方法取一个元素
NSString *str2 = arr[3];
//取多个
//NSIndexSet:是一个索引集合,原理上与数组类似,只不过这是一个无序的
NSIndexSet *set4 = [[NSIndexSet alloc]initWithIndexesInRange:NSMakeRange(1, 2)];
NSArray *arr3 = [array1 objectAtIndex:set4];
//简单方法截取多个元素
NSArray *arr4 = [array1 subarrayWithRange:NSMakeRange(2, 3)];
//for in 快速遍历 只针对数组来说,in的前面是数组中的元素, in后面是要遍历的集合(数组/字典)
for (NSString *str in array1) {
NSLog(@"%@",str);
}
二.可变数组
1.创建
//创建空数组
NSMutableArray *mutArray = [NSMutableArray array];
//capacity
NSMutableArray *mutAr = [NSMutableArray arrayWithCapacity:100];
NSMutableArray *mutcap = [[NSMutableArray alloc]initWithCapacity:100];
NSArray *array = @[@"dskfj",@"df",@"sd",@"f"];
NSMutableArray *mutarr3 = [[NSMutableArray alloc]initWithArray:array];
//通过类方法
NSMutableArray *mutarr5 = [NSMutableArray arrayWithObjects:@"sdj",@"dsf", nil];
2.增删改查
//增
NSMutableArray *mutArray1 = [NSMutableArray arrayWithObjects:@"sunck", @"is", @"a", @"nice", @"man", nil];
//增加到数组的结尾
//1、增加一个元素
[mutArray1 addObject:@"!"];
NSLog(@"mutArray1-1 = %@", mutArray1);
//2、增加多个
[mutArray1 addObjectsFromArray:@[@"Oh", @"right"]];
NSLog(@"mutArray1-2 = %@", mutArray1);
//在数组中增加
//注意:如果数组中有多个相同的元素,那么会全部删除
//1、删除一个元素
//1.1按元素删除 注意:如果数组中有多个相同的元素,那么会全部删除
NSMutableArray *mutArray = [NSMutableArray arrayWithObjects:@"sd",@"sun",@"id",@"good",@"girl", nil];
[mutArray removeObject:@"sd"];
//1.2按下标删除
[mutArray removeObjectAtIndex:3];
//1.3删除最后一个
[mutArray removeLastObject];
//删除多个
//在range范围内删除第一个参数的中的数据
NSIndexSet *set4 = [[NSIndexSet alloc]initWithIndexesInRange:NSMakeRange(5, 2)];
[mutArray removeObjectsAtIndexes:set4];
//删除全部
[mutArray removeAllObjects];
3.字符传切割
//切割
NSString *str = @"sdjfkjdsjf#*sjkd#*skdud#*skudjhfksd#*";
NSArray *array = [str componentsSeparatedByString:@"#*"];
//一字符集合中的字符切割
NSString *str1 =@"skjdfsl!skljdfkds* smjdfjs*JHhndnf.sdfs";
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"!*,."];
NSArray *arr1 = [str1 componentsSeparatedByCharactersInSet:set];
//组合
//将数组中的字符传元素按照某种规则组合成一个新的字符串
NSArray *arr3 = @[@"sdjn",@"sun",@"id",@"sd"];
NSString *str4 = [arr3 componentsJoinedByString:@"!!"];
4.简单数据封装存储在字典和数字中
//NSNumber:专门用于封装简单数据类型的类
//Why:在OC中数组、字典等存储工具里面只能存储对象,所以需要封装
//封装成对象
int a = 10;
int b = 20;
NSNumber *aNum = [[NSNumber alloc] initWithInt:a];
NSNumber *bNum = [[NSNumber alloc] initWithInt:b];
NSLog(@"aNum = %@", aNum);
NSLog(@"bNum = %@", bNum);
//存入数组
NSArray *array = @[aNum, bNum];
//比较大小
NSComparisonResult result = [aNum compare:bNum];
//解封
int c = [aNum intValue];
int d = [bNum intValue];
5.数组排序
//数组的排序
//写排序的标准(方法):(升序排、降序排),写在数组元素的类里面
//NSOrderedDescending NSOrderedAscending
//带SEL的排序
[arr sortUsingSelector:NSOrderedDescending];