OC-Day3____数组
2015.3.2
//数组
//NSArray(不可变数组)
//数组:对象地址的有序集合
//
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSNumber *num1 = [NSNumber numberWithInt:12];
//数组对象创建
NSArray *array1 = @[@"one",@"two",@"three"];
NSLog(@"array1 = %@", array1);
NSArray *array2 = [[NSArray alloc] initWithObjects:@"two",@"one",@"three",@"four", nil];
NSLog(@"array2 = %@",array2);
//用传入的数组对象构造一个新的数组对象
NSArray *array3 = [[NSArray alloc] initWithArray:array2];
NSLog(@"array3 = %@", array3);
NSLog(@"array2 = %p array3 = %p", array2,array3);
NSArray *array4 = [[NSArray alloc] initWithArray:array1 copyItems:NO];
NSLog(@"array4 = %@", array4);
NSLog(@"array1 = %p, array4 = %p", array1, array4);
//类方法创建数组对象
NSArray *array5 = [NSArray arrayWithObjects:@"one",@"two",@"three", nil];
NSLog(@"array5 = %@",array5);
//利用C语言数组创建数组对象
NSString *carray[4]={@"one",@"two",@"three",@"four"};
NSArray *array6 = [NSArray arrayWithObjects:carray count:4];
NSLog(@"array6 = %@", array6);
NSArray *array7 = [NSArray arrayWithObject:@"five"];
NSLog(@"array7 = %@",array7);
//利用传入的数组对象创建一个新的数组对象
NSArray *array8 = [NSArray arrayWithArray:array6];
NSLog(@"array8 = %@", array8);
// + (NSArray *)arrayWithContentsOfFile:(NSString *)path;
// - (NSArray *)initWithContentsOfFile:(NSString *)path;
//要求文件根结构必须为数组
NSArray *fileArray = [NSArray arrayWithContentsOfFile:@"/Users/zhangxueming/Desktop/city.plist"];
NSLog(@"fileArray = %@", fileArray);
// + (NSArray *)arrayWithContentsOfURL:(NSURL *)url;
// - (NSArray *)initWithContentsOfURL:(NSURL *)url;
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
}
return 0;
}
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSArray *array = [NSArray arrayWithObjects:@"one",@"two",@"three",@"four",@"five", nil];
//统计数组元素个数
NSUInteger cnt = [array count];
NSLog(@"cnt = %lu", cnt);
//获取指定位置的对象
id obj = [array objectAtIndex:3];
NSLog(@"obj = %@", obj);
//添加数组元素
array = [array arrayByAddingObject:@"six"];
NSLog(@"array = %@", array);
array = [array arrayByAddingObjectsFromArray:@[@"seven",@"eight",@"nine"]];
NSLog(@"array = %@", array);
//用给定的字符串对象拼接数组元素
NSString *str = [array componentsJoinedByString:@" "];
NSLog(@"str = %@", str);
//判断数组中是否包含某个对象
BOOL ret = [array containsObject:@"ten"];
NSLog(@"ret = %d", ret);
//返回两个数组中第一个相同的对象
id obj1 = [array firstObjectCommonWithArray:@[@"ten",@"six",@"seven"]];
NSLog(@"obj1 = %@", obj1);
//返回对象在数组中第一次出现的位置
NSUInteger index = [array indexOfObject:@"five"];
NSLog(@"index = %lu", index);
//找不到返回NSNotFound
NSUInteger index1 = [array indexOfObject:@"five" inRange:NSMakeRange(3, 3)];
NSLog(@"index1 = %lu", index1);
//判断两个数组是否相等
BOOL ret1 = [array isEqualToArray:@[@"one",@"two",@"three"]];
NSLog(@"ret1 = %d", ret1);
//获取数组中第一个对象
id obj2 = [array firstObject];
NSLog(@"obj2 = %@", obj2);
//获取数组中最后一个对象
id obj3 = [array lastObject];
NSLog(@"obj3 = %@", obj3);
//数组提取
NSArray *subArray = [array subarrayWithRange:NSMakeRange(3, 4)];
NSLog(@"subArray = %@", subArray);
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(3, 4)];
//NSLog(@"indexSet = %@", indexSet);
NSArray *subArray1 = [array objectsAtIndexes:indexSet];
NSLog(@"subArray1 = %@", subArray1);
NSMutableIndexSet *mulIndex =[NSMutableIndexSet indexSet];//创建一个空的集合对象
[mulIndex addIndex:1];
[mulIndex addIndex:3];
[mulIndex addIndex:7];
[mulIndex addIndex:5];
NSArray *subArray2 = [array objectsAtIndexes:mulIndex];
NSLog(@"subArray2 = %@", subArray2);
}
return 0;
}
//NSMutableArray
//创建可变数组对象
//继承与NSArray
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSMutableArray *mulArray = [NSMutableArray arrayWithObjects:@"one", @"two", @"three", @"four", nil];
NSLog(@"mulArray = %@", mulArray);
//增加数组元素
[mulArray addObject:@"five"];
NSLog(@"mulArray = %@", mulArray);
//在指定位置插入元素
[mulArray insertObject:@"six" atIndex:2];
NSLog(@"mulArray = %@", mulArray);
//删除最后一个元素
[mulArray removeLastObject];
NSLog(@"mulArray = %@", mulArray);
//删除指定位置的元素
[mulArray removeObjectAtIndex:2];
NSLog(@"mulArray = %@", mulArray);
//替换指定位置的元素
[mulArray replaceObjectAtIndex:2 withObject:@"ten"];
NSLog(@"mulArray = %@", mulArray);
//创建指定容量大小的可变数组对象
NSMutableArray *mulArray1 = [NSMutableArray arrayWithCapacity:20];
[mulArray1 addObject:@"qianfeng"];
NSLog(@"mulArray1 = %@", mulArray1);
//添加数组元素
[mulArray1 addObjectsFromArray:mulArray];
NSLog(@"mulArray1 = %@", mulArray1);
//交换数组中的两个元素
[mulArray1 exchangeObjectAtIndex:3 withObjectAtIndex:4];
NSLog(@"mulArray1 = %@", mulArray1);
//删除数组中的所有元素
[mulArray1 removeAllObjects];
NSLog(@"mulArray1 = %@", mulArray1);
//在指定范围内删除指定的数组元素
[mulArray removeObject:@"ten" inRange:NSMakeRange(1, 2)];
NSLog(@"mulArray = %@", mulArray);
[mulArray addObject:@"one"];
[mulArray addObject:@"two"];
[mulArray addObject:@"two"];
NSLog(@"mulArray = %@", mulArray);
//删除数组中指定的元素
[mulArray removeObject:@"two"];
NSLog(@"mulArray = %@", mulArray);
[mulArray addObject:@"three"];
[mulArray addObject:@"five"];
[mulArray addObject:@"six"];
NSLog(@"mulArray = %@", mulArray);
//删除数组中所有在otherArray中出现的元素
[mulArray removeObjectsInArray:@[@"one",@"three",@"four"]];
NSLog(@"mulArray = %@", mulArray);
//删除指定范围内的元素
[mulArray addObject:@"three"];
[mulArray addObject:@"five"];
[mulArray addObject:@"six"];
NSLog(@"mulArray = %@", mulArray);
[mulArray removeObjectsInRange:NSMakeRange(2, 2)];
NSLog(@"mulArray = %@", mulArray);
//用传入指定范围内的数组元素替换目标范围内的指定数组元素
//- (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray range:(NSRange)otherRange;
NSMutableArray *mulArr1 = [NSMutableArray arrayWithObjects:@"one",@"two",@"three",@"four",@"five",nil];
NSMutableArray *mulArr2 = [NSMutableArray arrayWithObjects:@"qian",@"feng",@"hello",@"world", nil];
[mulArr1 replaceObjectsInRange:NSMakeRange(1, 3) withObjectsFromArray:mulArr2 range:NSMakeRange(2, 2)];
NSLog(@"mulArr1 = %@", mulArr1);
//修改(重置)数组元素
[mulArr1 setArray:@[@"qian",@"feng",@"jiao",@"xue"]];
NSLog(@"mulArr1 = %@", mulArr1);
//在数组中集合位置添加数组元素
NSMutableIndexSet *mulIndex3 = [NSMutableIndexSet indexSet];
[mulIndex3 addIndex:0];
[mulIndex3 addIndex:2];
[mulIndex3 addIndex:4];
[mulArr1 insertObjects:@[@"one",@"two",@"three"] atIndexes:mulIndex3];//传入的数组元素个数 要与位置集合元素个数对象
NSLog(@"mulArr1= %@", mulArr1);
//删除位置集合内的所有元素
//注意要删除的元素在数组范围内
[mulArr1 removeObjectsAtIndexes:mulIndex3];
NSLog(@"mulArr1= %@", mulArr1);
//- (void)replaceObjectsAtIndexes:(NSIndexSet *)indexes withObjects:(NSArray *)objects;
//用给定的数组替换指定位置集合的元素
NSMutableIndexSet *mulIndex4 = [NSMutableIndexSet indexSet];
[mulIndex4 addIndex:1];
[mulIndex4 addIndex:3];
[mulArr1 replaceObjectsAtIndexes:mulIndex4 withObjects:@[@"one",@"two"]];
NSLog(@"mulArr1 = %@", mulArr1);
}
return 0;
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。