1.数组的创建方式
不可变数组:一旦创建成功,内容不可改变 NSMutableArray *array11 = [[NSMutableArray alloc]init]; NSMutableArray *array12 = [[NSMutableArray alloc]initWithObjects:@"1",nil]; NSMutableArray *array13 = [[NSMutableArray alloc]initWithArray:@[@1,@2]]; NSMutableArray *array14 = [NSMutableArray new]; 可变数组:创建成功 NSArray *array21 = [[NSArray alloc]init]; NSArray *array22 = [[NSMutableArray alloc]initWithArray:@[@1,@1]]; NSArray *array23 = [[NSArray alloc]initWithObjects:@"2",@"3", nil]; NSArray *array24 = [NSArray new];
2.获取数组中的元素个数以及访问数组元素
NSMutableArray *array = [[NSMutableArray alloc]init]; [array11 addObjectsFromArray:@[@1,@2,@3]]; NSLog(@"数组的个数=%lu",(unsigned long)array.count); NSString *string = [array objectAtIndex:0]; NSString *string = array[0]; NSLog(@"array中的第一个元素是\n%@",string);
3.追加数组中的内容(可变的才可追加或者生成新的数组)
NSArray *array = [NSArray arrayWithObjects:@"1",@"2",nil]; NSArray *array1 = [array arrayByAddingObject:@"3"]; NSLog(@"%@",array1);
4.把数组转化为字符串拼接起来
NSArray *array = [NSArray arrayWithObjects:@"I",@"Love",@"You", nil]; NSString *string1 = [array componentsJoinedByString:@"❤️"]; NSLog(@"string9 : %@",string1);
5.把字符串转化为数组(根据特殊符来分割成数组)
NSString *string1 =@"I❤️Love❤️You"; NSArray *array = [string1 componentsSeparatedByString:@"❤️"]; NSLog(@"\n\n分割的数组=%@",array);
6. 判断数组中是否存在一个指定的对象
NSArray *array = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",nil]; BOOL isContain = [array containsObject:@"3"]; if (isContain) { NSLog(@"\n\n包含这个元素"); }else{ NSLog(@"\n\n不包含这个元素"); }
7.根据指定的对象返回索引下标,返回数组中最后一个元素以及第一个元素
NSArray *array = [NSArray arrayWithObjects:@"1",@"2",nil]; NSInteger index = [array indexOfObject:@"2"]; NSLog(@"包含元素的下标 = %ld",index);
NSArray *array = [NSArray arrayWithObjects:@"1",@"2",nil]; NSString *lastObject = [array lastObject]; NSLog(@"最后一个元素 = %@",lastObject);
NSArray *array = [NSArray arrayWithObjects:@"1",@"2",nil]; NSString *firstObject = [array firstObject]; NSLog(@"\n\n\n第一个元素 = %@",firstObject);
8.可变数组的常用(新增元素)
NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",nil]; [mutableArray addObject:@"5"]; NSLog(@"新增后的结果 = %@",mutableArray);
9.可变数组的插入
NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",nil]; [mutableArray insertObject:@"5" atIndex:1];// 插入数组的下表 NSLog(@"\n\n\n插入数组后的结果 = %@",mutableArray);
10.删除最后一个元素
NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",nil]; [mutableArray removeLastObject]; NSLog(@"\n\n\n移除最后一个数组元素后的结果 = %@",mutableArray);
11.删除指定的元素(根据对象删除)
NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",nil]; [mutableArray removeObject:@"2"]; NSLog(@"\n\n\n移除指定元素后的结果 = %@",mutableArray);
12.删除指定位置的元素(根据下表移除)
NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",nil]; [mutableArray removeObjectAtIndex:2]; NSLog(@"\n\n\n移除指定位置元素后的结果 = %@",mutableArray);
13.批量删除元素
NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",nil]; NSArray *deleteArray = [NSArray arrayWithObjects:@"1", @"2", nil]; [mutableArray removeObjectsInArray:deleteArray]; NSLog(@"\n\n\n批量删除元素后的结果 = %@",mutableArray);
14.替换元素
NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",nil]; [mutableArray replaceObjectAtIndex:2 withObject:@"8"]; NSLog(@"\n\n\n指定替换元素后的结果 = %@",mutableArray);
15.遍历数组(两种遍历方式)
1.第一种遍历 NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",nil]; for (int index = 0; index < mutableArray.count; index++) { NSLog(@"数组里第%d个元素是 : %@",index+1,mutableArray[index]); }
2.第二种遍历
NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",nil]; [mutableArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { NSLog(@"数组里面第%ld的值=%@",idx+1,obj); }];
16.取出连续范围的元素作为子集内容
NSArray *array = @[@"111", @"222", @"333", @"444", @"555", @"666"]; //取出连续范围的元素作为子集内容 NSArray *sub1 = [array subarrayWithRange:NSMakeRange(1, 3)]; NSLog(@"\n\n指定范围内的元素 = \n%@", sub1);
17.交换数组中得内容
NSMutableArray *array = [[NSMutableArray alloc]initWithArray:@[@"111", @"222", @"333", @"444", @"555", @"666"]]; [array exchangeObjectAtIndex:1 withObjectAtIndex:3]; NSLog(@"\n\n交换元素后的数组 = \n%@", array);
18.用oc对c语言中得数组进行排序(这里仅仅对于长度排序)
NSMutableArray *array = [[NSMutableArray alloc]initWithArray:@[@"111111", @"22222", @"3333", @"444", @"55", @"6"]]; //用oc对c语言中得数组进行排序 for (int i = 0; i < [array count]; ++i) { for (int j = i + 1; j < [array count]; ++j) { if ([array[i] length] > [array[j] length]) { [array exchangeObjectAtIndex:i withObjectAtIndex:j]; } } } NSLog(@"\n\n排序后的数组=\n\narr2 = \n%@",array);