OC-数组类
NSArray
创建一个不可变数组对象
NSMutableArray
创建可变数组对象
数组中的元素都是对象可以是任意类型的对象
/*数组中存放的对象的引用*/
/* 数组的初始化方法 :NSArray 创建的数组不能被修改*/
1.OC中的数组初始化
包括类方法初始化和对象方法初始化两种方式:
1 int main(int argc, const char * argv[]) { 2 @autoreleasepool { 3 //直接创建一个常量数组 4 NSArray *array1 = @[@"one",@"two",@"three"]; 5 NSLog(@"array1 = %@",array1); 6 7 /* 8 OC中的数组类似于C语言中的指针数组,但是指针数组只能同一类型的,而OC中的数组可以是任意类型的 9 */ 10 //用不同对象来初始化数组 11 NSArray *array2 = [[NSArray alloc]initWithObjects:@"hello world", [NSNumber numberWithInt:123],[NSDate date],[NSNumber numberWithFloat:3.14],nil]; 12 NSLog(@"array2 = %@",array2); 13 14 //用其他已经存在的数组来创建数组 15 NSArray *array3 = [[NSArray alloc]initWithArray:array1]; 16 NSLog(@"array3 = %@",array3); 17 18 //copyItems:拷贝一份 19 //copyItems:直接return 20 NSArray *array4 = [[NSArray alloc]initWithArray:array2 copyItems:YES]; 21 NSLog(@"array4 = %@",array4); 22 23 //类方法 24 //创建一个空的数组对象 25 NSArray *array5 = [NSArray array]; 26 NSLog(@"array5 = %@",array5); 27 28 //多个对象创建一个数组 29 NSArray *array6 = [NSArray arrayWithObjects:@"one",@"two",[NSNumber numberWithFloat:3.14],nil]; 30 NSLog(@"array6 = %@",array6); 31 32 //用已经存在的数组创建数组 33 NSArray *array7 = [NSArray arrayWithArray:array6]; 34 NSLog(@"array7 = %@",array7); 35 } 36 return 0; 37 }
2.OC数组的操作函数
1 int main(int args,const char *argv[]) 2 { 3 @autoreleasepool { 4 NSArray *array1 = [[NSArray alloc]initWithObjects:@"123",@"abc",@"oop", nil]; 5 NSLog(@"array1 = %@",array1); 6 7 //计算数组的长度 8 NSInteger count = [array1 count]; 9 NSLog(@"count = %ld",count); 10 11 //获取指定位置的对象 12 id obj = [array1 objectAtIndex:2]; 13 NSLog(@"obj = %@",obj); 14 15 //在数组中添加一个对象:其实是在底层中生成一个新的数组对象 16 NSArray *array2 = [array1 arrayByAddingObject:@"alibb"]; 17 NSLog(@"array2 = %@",array2); 18 19 //将传入的数组添加到一个数组的末尾 20 NSArray *otherArray = [[NSArray alloc]initWithObjects:@"add",@"sub",@"mul",@"div", nil]; 21 NSArray *array3 = [array1 arrayByAddingObjectsFromArray:otherArray]; 22 NSLog(@"array3 = %@",array3); 23 24 //用指定字符串连接数组元素 25 NSLog(@"array1 = %@",array1); 26 NSString *str = [array1 componentsJoinedByString:@"##"]; 27 NSLog(@"str = %@",str); 28 29 //判断是否包含对象 30 BOOL flag = [array1 containsObject:@"oop"]; 31 NSLog(@"flag = %i",flag); 32 33 //返回两个数组中第一个公共的对象 34 id obj2 = [array1 firstObjectCommonWithArray:otherArray]; 35 NSLog(@"obj2 = %@",obj2); 36 37 38 //返回数组中第一次出现的下标位置 39 NSArray *array4 = [[NSArray alloc]initWithObjects:@"add",@"sub",@"mul",@"zjl",@"mul",@"div", nil]; 40 NSUInteger index = [array4 indexOfObject:@"mul"]; 41 NSLog(@"index = %lu",index); 42 //在数组的指定范围内查找第一次出现的对象,并返回下标 43 NSUInteger indexRange = [array4 indexOfObject:@"mul" inRange:NSMakeRange(3, 3)]; 44 NSLog(@"index2 = %lu",indexRange); 45 46 //判断两个数组是否相等 47 BOOL flag1 = [array1 isEqualToArray:array4]; 48 NSLog(@"flag1 = %i",flag1); 49 50 //获取数组中的第一个元素 51 id obj3 = [array4 firstObject]; 52 NSLog(@"obj3 = %@",obj3); 53 54 //获取数组中的最后一个元素 55 id obj4 = [array4 lastObject]; 56 NSLog(@"ojb4 = %@",obj4); 57 58 59 //重要:数组的遍历 有三种方法 60 //1.下标法 61 NSArray *array5 = @[@"one",@"two",@"three",@"foure",@"five"]; 62 NSUInteger len = [array5 count]; 63 for (int i=0; i<len; i++) { 64 NSLog(@"array5[%i] = %@",i,[array5 objectAtIndex:i]); 65 } 66 //2.枚举器法 67 NSEnumerator *enumerator = [array5 objectEnumerator]; //生成枚举器对象 68 //通过枚举器对象来遍历对象 69 id item = nil; 70 while (item = [enumerator nextObject]) { 71 NSLog(@"%@",item); 72 } 73 //通过枚举器法反向变量数组 74 NSEnumerator *reverseEnumerator = [array5 reverseObjectEnumerator]; 75 id item1 = nil; 76 while (item1 = [reverseEnumerator nextObject]) { 77 NSLog(@"item1 = %@",item1); 78 } 79 //3.快速枚举法 //速度最快 80 for (id item2 in array5) { 81 NSLog(@"item2 = %@",item2); 82 } 83 84 //数组排序,通过选择器 85 NSArray *array6 = @[@"one",@"two",@"three",@"four",@"five"]; 86 NSArray *array7 = [array6 sortedArrayUsingSelector:@selector(compare:)]; 87 NSLog(@"array7 = %@",array7); 88 89 //数组提取 90 NSArray *array8 = [array6 subarrayWithRange:NSMakeRange(1, 3)]; 91 NSLog(@"array8 = %@",array8); 92 93 94 //通过对象下标集合获取指定的数组对象 95 //创建一个下标集合 96 NSMutableIndexSet *indexSet = [[NSMutableIndexSet alloc] initWithIndex:0]; 97 [indexSet addIndex:2]; 98 [indexSet addIndex:4]; 99 [indexSet addIndex:0]; 100 //通过对象下标集合获取指定的数组对象 101 NSArray *array9 = [array6 objectsAtIndexes:indexSet]; 102 NSLog(@"array9 = %@",array9); 103 } 104 }
3.OC可变数组的操作函数(特有的)
1 int main(int args,const char *argv[]) 2 { 3 @autoreleasepool { 4 NSMutableArray *mulArray = [[NSMutableArray alloc] initWithCapacity:20]; 5 NSMutableArray *mulArray1 = [NSMutableArray arrayWithCapacity:20]; 6 7 8 [mulArray addObject:@"one"]; 9 NSLog(@"mulArray = %@",mulArray); 10 11 //指定位置中插入对象 12 [mulArray insertObject:@"two" atIndex:1]; 13 NSLog(@"mulArray = %@",mulArray); 14 15 //将给定的数组插入到可变数组末尾 16 [mulArray addObjectsFromArray:@[@"123",@"567",@"890"]]; 17 NSLog(@"mulArray = %@",mulArray); 18 19 //删除数组最后一个元素 20 [mulArray removeLastObject]; 21 NSLog(@"mulArray = %@",mulArray); 22 23 //从数组删除指定对象 24 [mulArray removeObject:@"123"]; 25 NSLog(@"mulArray = %@",mulArray); 26 27 //删除数组中指定位置的元素 28 [mulArray removeObjectAtIndex:1]; 29 NSLog(@"mulArray = %@",mulArray); 30 31 //替换指定位置的元素 32 [mulArray replaceObjectAtIndex:0 withObject:@"five"]; 33 NSLog(@"mulArray = %@",mulArray); 34 35 //交换两个位置的元素 36 [mulArray exchangeObjectAtIndex:0 withObjectAtIndex:1]; 37 NSLog(@"mulArray = %@",mulArray); 38 39 [mulArray addObjectsFromArray:@[@"aaa",@"ccc",@"bbb",@"ccc",@"ddd",@"eee",@"fff"]]; 40 41 //在指定范围内删除指定对象 42 [mulArray removeObject:@"567" inRange:NSMakeRange(0, 1)]; 43 NSLog(@"mulArray = %@",mulArray); 44 45 //删除数组中指定的所有对象 46 [mulArray removeObject:@"ccc"]; 47 NSLog(@"mulArray = %@",mulArray); 48 49 //删除所有在传入数组中出现的元素 50 [mulArray removeObjectsInArray:@[@"aaa",@"bbb",@"888"]]; 51 NSLog(@"mulArray = %@",mulArray); 52 53 //删除所有在指定范围内的元素 54 [mulArray removeObjectsInRange:NSMakeRange(1, 2)]; 55 NSLog(@"mulArray = %@",mulArray); 56 57 58 //用给定的数组 修改数组(重置)数组 59 [mulArray setArray:@[@"123",@"345",@"567",@"789"]]; 60 NSLog(@"mulArray = %@",mulArray); 61 62 63 //数组排序 64 NSMutableArray *sortArray = [NSMutableArray array]; 65 [sortArray sortUsingSelector:@selector(compare:)]; 66 NSLog(@"sortArray = %@",sortArray); 67 68 69 //用传入的数组替换指定范围内的数组元素 70 //用传入的数组替换指定范围内的数组元素 71 [mulArray replaceObjectsInRange:NSMakeRange(0, 2) withObjectsFromArray:@[@"one",@"two",@"three"]]; 72 NSLog(@"mulArray = %@", mulArray); 73 } 74 }
内容原创-可转载-请保留原地址