// 数组的初始化方式
// 不可变数组,数组内不可添加删除替换等元素
// 1.初始化一个空数组
NSArray *array = [[NSArray alloc]init];
NSArray *array1 = [NSArray array];
// 2.初始化的同时添加元素
NSArray *array2 = [NSArray arrayWithObject:@”“];//数组里可放任意类型数据
NSArray *array3 = [NSArray arrayWithObjects:@”“, @”“, nil];//nil前面有个逗号,不要忘记写nil
// 3.新方法
NSArray *array4 = @[@”15”,@”13”];
NSArray *list = [NSArray arrayWithObjects:@”章子怡”,@”林依晨”,@”刘诗诗”,@”VAE”,@”周杰伦”, nil];
// 数组取值
// 通过数组的下标取值,下标从0开始
NSString *jjj = [list objectAtIndex:0];
NSLog(@”%@”,jjj);
// 取值新方法
NSString *name6 = list[2];
pragma mark —————练习1—————
// 把附近的人写到一个数组,逐个取出人名
NSArray *name = [NSArray arrayWithObjects:@”小旺”,@”丁丁”,@”小龙”,@”波波”,@”小诚”, nil];
NSString *name1 = name[0];
NSString *name2 = name[1];
NSString *name3 = name[2];
NSString *name4 = [name objectAtIndex:3];
NSString *name5= [name objectAtIndex:4];
NSLog(@”\n%@\n%@\n%@\n%@\n%@”,name1,name2,name3,name4,name5);
pragma mark ———数组练习———-
// 1.把四季的季节名存到数组里,按下标逐个取出
NSArray *season = [NSArray arrayWithObjects:@”spring”,@”summer”,@”fall”,@”winter”, nil];
NSString *season1 = [season objectAtIndex:0];
NSString *season2 = [season objectAtIndex:1];
NSString *season3 = [season objectAtIndex:2];
NSString *season4 = [season objectAtIndex:3];
NSLog(@”\n%@\n%@\n%@\n%@\n”,season1,season2,season3,season4);
// 2. 写一个英雄的数组,让用户通过数组的下标选择英雄
NSArray *hero = [NSArray arrayWithObjects:@”战士”,@”法师”,@”术士”,@”格斗”,@”剑士”, nil];
NSString *hero1 = [hero objectAtIndex:0];
NSString *hero2 = [hero objectAtIndex:1];
NSString *hero3 = [hero objectAtIndex:2];
NSString *hero4 = [hero objectAtIndex:3];
NSString *hero5 = [hero objectAtIndex:4];
NSLog(@”本款游戏有以下英雄:%@\n%@\n%@\n%@\n%@\n”,hero1,hero2,hero3,hero4,hero5);
NSLog(@”请输入你选择的英雄:1:战士 2:法师 3.术士 4.格斗 5.剑士”);
NSInteger choose;
scanf(“%ld”,&choose);
NSLog(@”您选择的英雄是:%@\n”,hero[choose - 1]);
pragma mark —————数据类型转换—————-
// 数组里面需要的是id类型,需要把基本数据类型转换成NSNumber类型
int age = 120;
NSNumber *ageNum = [[NSNumber alloc] initWithInt:age];//NS开头,都需要一个*
NSNumber *ageNum3 = [[NSNumber alloc] initWithInteger:age];
NSNumber *ageNum4 = [[NSNumber alloc] initWithDouble:age];
NSLog(@”%@\n”,ageNum);
NSLog(@”%@\n”,ageNum3);
NSLog(@”%@\n”,ageNum4);
NSNumber *ageNum1 = [NSNumber numberWithInt:age];
NSLog(@"%@\n",ageNum1);
NSNumber *ageNum2 = [NSNumber numberWithFloat:age];
NSLog(@"%@\n",ageNum2);
float height = 180.5;
NSNumber *heightNum = [NSNumber numberWithFloat:height];
NSLog(@"%@",heightNum);
float weight = 30.5;
NSNumber *ageNumber = @(weight);// @(转换的数据,忽略数据类型)
NSLog(@"%@\n",ageNumber);
// 转回
float aa = [ageNum integerValue];
[ageNum floatValue];
[ageNum intValue];
[ageNum doubleValue];
pragma mark —————练习————–
// 写一个存着分数的数组,并把分数取出,转成float类型
float Math = 89.5;
float Chinese = 100;
double English = 70.6;
NSNumber *MathNum = @(Math);
//把80转成NSNumber类型
NSArray *nn = @[@(80)];
NSArray *scorelist = @[@(Math),@(Chinese),@(English)];
NSArray *countList = @[[NSNumber numberWithFloat:Math],[NSNumber numberWithFloat:Chinese],[NSNumber numberWithFloat:English]];
NSLog(@”%f\n%f\n%f\n”,Math,Chinese,English);