//oc的数组必须存放对象 可以是多种对象类型 但是实际操作中 尽量在一个数组中存放一种对象类型
//NSArray数组的创建
//创建空数组: + (instancetype)array;
NSArray *array = [NSArray array];
// NSArray *array = [[NSArray alloc]init];
// NSArray *array = [NSArray new];
NSLog(@"%@",array);
//类似于c语言创建数组并赋值
NSArray *arr = @[@"one",@"two",@"xmy",@1];
NSLog(@"%@",arr);
//创建数组并且数组元素只有一个
//+ (instancetype)arrayWithObject:(ObjectType)anObject;
NSArray *array2=[NSArray arrayWithObject:@"xmy"];
NSLog(@"%@",array2);
//调用类方法创建数组并给其赋值 以nil作为初始化结束 所以不要在数组中添加值为nil的对象
//+ (instancetype)arrayWithObjects:(ObjectType)firstObj, ...
NSArray *array1 = [NSArray arrayWithObjects:@"one",@"小王",@1 ,nil];
NSLog(@"%@",array1);
//调用对象方法创建数组
//- (instancetype)initWithObjects:(ObjectType)firstObj, ... NS_REQUIRES_NIL_TERMINATION;
NSArray *array3=[[NSArray alloc]initWithObjects:@"one",[NSNull null ],@"two", nil];//[NSNull null]: "<null>"
NSLog(@"%@",array3);
//用一个数组创建另一个数组
//+ (instancetype)arrayWithArray:(NSArray<ObjectType> *)array;
NSArray *array4 =[NSArray arrayWithArray:array3];
// NSArray *array4 = [[NSArray alloc] initWithArray:array3]
NSLog(@"%@",array4);//值同array3
NSArray *array5=[NSArray arrayWithObjects:@"one",@"two",@"three",@2, nil];
//@property (readonly) NSUInteger count;获取数组元素的个数
NSLog(@"%lu",array5.count);
//- (ObjectType)objectAtIndex:(NSUInteger)index;根据下标获取下标对应的数组元素
NSLog(@"%@",[array5 objectAtIndex:0]);//输出:one
//- (NSString *)componentsJoinedByString:(NSString *)separator;
//将数组中的元素用某个符号(例如:“-”,“.”)连接起来形成一个字符串
NSString * str1 = [array5 componentsJoinedByString:@"-"];
NSLog(@"%@",str1);
//相反地 可以将字符串分离成一个数组
NSString *u1 =@"www.goole.com";
NSArray *uarray = [u1 componentsSeparatedByString:@"."];
NSLog(@"%@",uarray);
//- (BOOL)containsObject:(ObjectType)anObject;判断数组中是否包含某元素
if ([array5 containsObject:@"xmy"]) {
NSLog(@"包含此元素");
}else{
NSLog(@"不包含");
}
//- (NSUInteger)indexOfObject:(ObjectType)anObject;根据对象返回该对象在数组中的索引值 如果不存在返回值为-1
NSUInteger loc = [array5 indexOfObject:@"three"];
NSLog(@"%lu",loc);
//获取该数组的第一个元素或者最后一个元素
NSString*a= array5.firstObject;
NSString *b = array5.lastObject;
NSLog(@"%@ %@",a,b);
// NSMutableArray 可变数组
//创建:
//NSMutableArray *m2 =[[NSMutableArray alloc]init];
//NSMutableArray *m2 = [NSMutableArray new];
// NSMutableArray *m2 = [NSMutableArray array];
//NSMutableArray *m = [[NSMutableArray alloc]initWithCapacity:6];
// NSMutableArray *m1 = [NSMutableArray arrayWithCapacity:6];//之后仅能对其做增删插运算 不能再使用初始化方法对其赋值
NSMutableArray *m = [NSMutableArray arrayWithObjects:@"xmy", @"hyt",@"ls",nil];
// - (void)addObject:(ObjectType)anObject;只能添加非空对象
[m addObject:@"wt"];
// [m addObject:nil];系统警告
NSLog(@"%@",m);
// - (void)insertObject:(ObjectType)anObject atIndex:(NSUInteger)index;在某个位置插入某个对象
[m insertObject:@"" atIndex:0];
NSLog(@"%@",m);
//- (void)removeLastObject;删除最后一个对象
[m removeLastObject];
NSLog(@"%@",m);
// - (void)removeObjectAtIndex:(NSUInteger)index;删除某个特定位置的对象
[m removeObjectAtIndex:2];
NSLog(@"%@",m);
// - (void)replaceObjectAtIndex:(NSUInteger)index withObject:(ObjectType)anObject;代替某个指定位置的对象
[m replaceObjectAtIndex:0 withObject:@"hyt"];
NSLog(@"%@",m);
// - (void)addObjectsFromArray:(NSArray<ObjectType> *)otherArray;将一个数组中的对象添加进另一个数组
NSArray *arrayt=@[@"ac"];
[m addObjectsFromArray:arrayt];
NSLog(@"%@",m);
// - (void)exchangeObjectAtIndex:(NSUInteger)idx1withObjectAtIndex:(NSUInteger)idx2;交换数组中两个元素的位置
[m exchangeObjectAtIndex:0 withObjectAtIndex:1];
NSLog(@"%@",m);
// - (void)removeObject:(ObjectType)anObject;删除某个指定的对象
[m removeObject:@"ac"];
NSLog(@"%@",m);
// - (void)removeAllObjects;删除数组中的所有对象
[m removeAllObjects];
NSLog(@"%@",m);
}
return 0;
}