//使用arrayWithObjects创建NSArray
NSArray *arr = [NSArray arrayWithObjects:@"abc",@"def",@"123",nil];
// NSArray *arr = [[NSArray alloc] initWithObjects:@"111",@"ddd",@"234",nil];
//使用arrayWithArray创建NSArray
NSArray *arr1 = [NSArray arrayWithArray:arr];
// NSArray *arr1 = [[NSArray alloc]initWithArray:arr];
//使用@创建NSArray,只能创建不可变数组
NSArray *arr2 = @[@"aaaa",@"bbb",@"ddddd"];
NSLog(@"arr2 = %@",arr2);
//返回元素个数
NSLog(@"arr2 count = %ld",[arr2 count]);
//返回指定位置id
NSString *str0 = [arr2 objectAtIndex:2];
NSLog(@"str0 = %@",str0);
//访问数组的最后一个元素
NSString *str = [arr2 lastObject];
NSLog(@"str = %@",str);
//访问数组的第一个元素
NSString *str1 = [arr firstObject];
NSLog(@"str1 = %@",str1);
//判断数组内是否含有某一个对象
if([arr containsObject:@"123"]) {
NSLog(@"含有此对象");
}
//获取某个对象在数组中的下标值
NSUInteger index = [arr indexOfObject:@"123"];
//数组的遍历
for(NSInteger i =0;i < [arr count]; i++) {
NSLog(@"arr[%ld] = %@",i,arr[i]);
}
//快速枚举法(快速遍历)
for(id objin arr) {
NSLog(@"obj = %@",obj);
}
int main(int argc, char * argv[]) {
//使用arrayWithObjects创建NSArray
NSArray *arr = [NSArray arrayWithObjects:@"ABC",@"DEF",@"GHI",nil];
//NSArray *arr = [[NSArray alloc] initWithObjects:@"ABC",@"DEF",@"GHI",nil];
NSLog(@"arr = %@",arr);
//使用arrayWithArray创建NSArray
NSArray *arr1 = [NSArray arrayWithArray:arr];
//NSArray *arr1 = [[NSArray alloc]initWithArray:arr];
NSLog(@"arr1 = %@",arr1);
//使用@创建NSArray,只能创建不可变数组
NSArray *arr2 = @[@"123",@"456",@"789"];
NSLog(@"arr2 = %@",arr2);
//返回元素个数
NSLog(@"arr count = %ld",[arr count]);
//返回指定位置id
NSString *str = [arr objectAtIndex:1];
NSLog(@"str = %@",str);
//访问数组的第一个元素
NSString *strF = [arr firstObject];
NSLog(@"strF = %@",strF);
//访问数组的最后一个元素
NSString *strL = [arr lastObject];
NSLog(@"strL = %@",strL);
//判断数组内是否含有某一个对象
if ([arr2 containsObject:@"456"]) {
NSLog(@"数组arr2内含有对象456");
}
//获取某个对象在数组中的下标值
NSUInteger index = [arr indexOfObject:@"ABC"];
NSLog(@"ABC下标值为%ld",index);
//数组的遍历
for (NSInteger i = 0; i < [arr count]; i++) {
NSLog(@"arr[%ld] = %@",i,arr[i]);
}
//快速枚举法(快速遍历)
for (id arr2Q in arr2) {
NSLog(@"arr2Q = %@",arr2Q);
}
}
//实例化一个空的可变数组
NSMutableArray *nsarr = [[NSMutableArray alloc]init];
//添加一个元素
[nsarr addObject:@"ONE"];
NSLog(@"nsarr = %@",nsarr);
//添加多个元素
[nsarr addObjectsFromArray:@[@"ONE",@"TWO",@"THREE"]];
NSLog(@"nsarr = %@",nsarr);
//插入元素
[nsarr insertObject:@"Two" atIndex:1];
NSLog(@"nsarr = %@",nsarr);
//替换元素
[nsarr replaceObjectAtIndex:0 withObject:@"1"];
NSLog(@"nsarr = %@",nsarr);
//查询元素
NSString *result = [nsarr objectAtIndex:1];//arr[1]
NSLog(@"result = %@",result);
//删除指定元素
[nsarr removeObject:@"ONE"];
NSLog(@"nsarr = %@",nsarr);
//删除指定下标的元素
[nsarr removeObjectAtIndex:2];
NSLog(@"nsarr = %@",nsarr);
//删除最后一个元素
[nsarr removeLastObject];
NSLog(@"nsarr = %@",nsarr);
//删除所有元素
[nsarr removeAllObjects];
NSLog(@"nsarr = %@",nsarr);
//删除数组中含有的所有元素
[nsarr removeObjectsInArray:@[@"1",@"THREE"]];
//交换元素
NSMutableArray *nsarr1 = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",nil];
[nsarr1 exchangeObjectAtIndex:1 withObjectAtIndex:2];
NSLog(@"nsarr1 = %@",nsarr1);
//切割字符串
NSString *str3 = @"I am a super man + beautiful, yeah!";
//用指定的字符串进行切割
NSArray *rArr = [str3 componentsSeparatedByString:@" "];
NSLog(@"rArr = %@",rArr);
//实例化一个字符集合
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@" ,+"];
//按照字符集合所提供的字符进行切割
NSArray *rArr2 = [str3 componentsSeparatedByCharactersInSet:set];
NSLog(@"rArr2 = %@",rArr2);
//拼接数组内的字符串
NSString *str2 = [rArr2 componentsJoinedByString:@"-"];
NSLog(@"str2 = %@",str2);
//按照ASCII码大小进行排序,SEL:方法选择器(储存一个方法名)
NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:@"B",@"A",@"C",@"Z",nil];
[arr3 sortedArrayUsingSelector:@selector(compare:)];//方法选择器
//按三个人年龄大小排序
Person *per0 = [[Person alloc]initWith:18];
Person *per1 = [[Person alloc]initWith:6];
Person *per2 = [[Person alloc]initWith:60];
NSMutableArray *perArr = [[NSMutableArray alloc]initWithObjects:per0,per1,per2,nil];
//compareAge提供一个排序方法,看按什么方式排序
NSArray *resultPerArr = [perArr sortedArrayUsingSelector:@selector(compareAge:)];
int main(int argc, char * argv[]) {
//实例化一个空的可变数组
NSMutableArray *arr = [[NSMutableArray alloc]init];
//添加一个元素
[arr addObject:@"he"];
NSLog(@"arr = %@",arr);
//添加多个元素
[arr addObjectsFromArray:@[@"is",@"Chinese"]];
NSLog(@"arr = %@",arr);
//插入元素
[arr insertObject:@"not" atIndex:2];
NSLog(@"arr = %@",arr);
//以arrayWithArray创建arr1
NSMutableArray *arr1 = [NSMutableArray arrayWithArray:arr];
//替换元素
[arr replaceObjectAtIndex:3 withObject:@"Japanese"];
NSLog(@"arr = %@",arr);
//查询元素
NSString *result = [arr objectAtIndex:0];
NSLog(@"arr[0] = %@",result);
//删除指定元素
[arr removeObject:@"not"];
NSLog(@"arr = %@",arr);
//删除指定下标的元素
[arr removeObjectAtIndex:1];
NSLog(@"arr = %@",arr);
//删除最后一个元素
[arr removeLastObject];
NSLog(@"arr = %@",arr);
//删除所有元素
[arr removeAllObjects];
NSLog(@"arr = %@",arr);
//删除数组中含有的所有元素
[arr1 removeObjectsInArray:@[@"not",@"is"]];
NSLog(@"arr1 = %@",arr1);
//交换元素
[arr1 exchangeObjectAtIndex:0 withObjectAtIndex:1];
NSLog(@"arr1 = %@",arr1);
//切割字符串
NSString *str1 = @"I am,a super man!";
//用指定的字符串进行切割
NSArray *cutArr = [str1 componentsSeparatedByString:@","];
NSLog(@"cutArr = %@",cutArr);
//实例化一个字符集合
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"super "];
//按照字符集合所提供的字符进行切割
NSArray *cutArr2 = [str1 componentsSeparatedByCharactersInSet:set];
NSLog(@"cutArr2 = %@",cutArr2);
//拼接数组内的字符串
NSString *str2 = [cutArr2 componentsJoinedByString:@"-"];
NSLog(@"str2 = %@",str2);
/*
//按照ASCII码大小进行排序,SEL:方法选择器(储存一个方法名)
NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:@"B",@"A",@"C",@"Z",nil];
[arr3 sortedArrayUsingSelector:@selector(compare:)]; //方法选择器
//按三个人年龄大小排序
Person *per0 = [[Person alloc]initWith:18];
Person *per1 = [[Person alloc]initWith:6];
Person *per2 = [[Person alloc]initWith:60];
NSMutableArray *perArr = [[NSMutableArray alloc]initWithObjects:per0,per1,per2,nil];
//compareAge提供一个排序方法,看按什么方式排序
NSArray *resultPerArr = [perArr sortedArrayUsingSelector:@selector(compareAge:)];
*/
}
注:NSArray和NSMutableArray只能存储OC对象。