标签:
①.实现方法:13个人,3个人一组,用一个大数组来管理每一个分组.
//1.原数组 NSArray *array = @[@1,@2,@3,@4,@5,@6,@7,@8,@9,@10,@11,@12,@13]; //2.原数组个数 NSInteger count = [array count]; //3.新建一个可变的数组1. NSMutableArray *newArray = [[NSMutableArray alloc] init]; //4.循环 for (NSInteger i = 0;i < count; i++) { //判断每3个,初始化一个可变数组2 if (0 == i % 3) { NSMutableArray *newArray1 = [[NSMutableArray alloc] initWithCapacity:1]; //将可变数组2添加到可变数组1中. [newArray addObject:newArray1]; [newArray1 release]; } //获取可变数组1中的最后一个元素,即可变数组2. //给可变数组2添加元素 [[newArray lastObject] addObject:array[i]]; } //输出 NSLog(@"%@",newArray); [newArray release];
1.crayons.txt中代码如下
Almond #EED9C4 Antique Brass #C88A65 Apricot #FDD5B1 Aquamarine #71D9E2 Asparagus #7BA05B Atomic Tangerine #FF9966 Banana Mania #FBE7B2 Beaver #926F5B Bittersweet #FE6F5E Black #000000 Blizzard Blue #A3E3ED Blue #0066FF Blue Bell #9999CC Blue Green #0095B6 Blue Violet #6456B7 Brick Red #C62D42 Brink Pink #FB607F Brown #AF593E Burnt Orange #FF7034 Burnt Sienna #E97451 Cadet Blue #A9B2C3 Canary #FFFF99 Caribbean Green #00CC99 Carnation Pink #FFA6C9 Cerise #DA3287 Cerulean #02A4D3 Chartreuse #FF9966 Chestnut #B94E48 Copper #DA8A67 Cornflower #93CCEA Cotton Candy #FFB7D5 Cranberry #DB5079 Dandelion #FED85D Denim #1560BD Desert Sand #EDC9AF Eggplant #614051 Electric Lime #CCFF00 Fern #63具体代码如下:
//获取文件路径 NSString *filePath = @"/Users/Object-c日常作业/crayons.txt";//文件地址 //读取文件中的字符串 NSString *oldString = [NSString stringWithContentsOfFile: filePath encoding:NSUTF8StringEncoding error:nil]; //先对文件进行操作去除每一行的内容. NSArray *array = [oldString componentsSeparatedByString:@"\n"]; // 1、使?用字典管理所有的颜?色,即字典中存储的是多个键值对, 颜?色名称为 key,16进制颜?色值(不带#)是value。 //初始化一个字典 NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init]; //遍历数组中的每一行 for (NSString *newString in array) { //对每一行进行分割,返回数组 NSArray *arr = [newString componentsSeparatedByString:@" #"]; //将数组中的值取出来,添加到数据字典. [dictionary setObject:arr[1] forKey: arr[0]]; } //2、取出所有的key,升序排列。 NSArray *allKey = [dictionary allKeys]; NSArray *newKey = [allKey sortedArrayUsingComparator:^(id obj1 ,id obj2) { return [(NSString *)obj1 compare :(NSString *)obj2]; }]; NSLog(@"--------%@",newKey); //3.取出所有的value,按照排序后的key排列。 //取出所有的value值 NSMutableArray *allValue = [[NSMutableArray alloc] init]; for (NSString *key in newKey) { NSString *value = [dictionary objectForKey:key]; [allValue addObject:value]; } //4.使?用?一个新的字典管理颜?色,对颜?色进?行分类管理,即: “A”,“B”,“C”... 即这个字典包含多个键值对,key是26个字?母, value是数组,数组?存放的是 Color对象(包含name和colorValue)。需要?己创建Color类。 //1.先遍历字典,取出对应的key和value值,存放在每个Color对象内. //新建立字典 NSMutableDictionary *newDictionary = [[NSMutableDictionary alloc] init]; // NSMutableArray *newArray = [[NSMutableArray alloc] init]; for (NSString *keys in dictionary) { //创建color对象,添加从字典中取出的每一个key和value值. Color *color = [[Color alloc] init]; [color setColorName:keys]; //添加colorName [color setColorCode:[dictionary objectForKey:keys]];//添加colorCode //获取Color对象的colorName的首字母, NSString *str = [[color colorName] substringToIndex:1]; //大小写转换 NSString *string = [str uppercaseString]; //NSLog(@"s======string=%@",string); //判断字典中是否存在key NSMutableArray *value = [newDictionary objectForKey:string];//键color大写字母当key //NSLog(@"s=======%@",value); //判断是否存在 if (value == nil) { //不存在 //建立数组 value = [NSMutableArray array]; //[newArray addObject:color]; [newDictionary setObject:value forKey:string]; } [value addObject:color]; } NSLog(@"%@",newDictionary); }color对象属性如下:
@property (nonatomic, retain) NSString *colorName; @property (nonatomic, retain) NSString *colorCode;
【学习ios之路:Objective-C】数组,字符串,字典的总和练习
标签:
原文地址:http://blog.csdn.net/zfx5130/article/details/42464151