从两个一一对应的数组中按要求获取数据

    今天项目中有个需求,有两个数组,一个存的是水果,另一个存的是对应水果的分数,他们的关系是一一对应的,也就是水果在fruits数组中的位置和它所对应的分数在scores数组中的位置相同。要求获取到分数最高的水果名,有两种解决思路:

思路一:将分数和水果一一关联,再从scores数组中获取到最大分数,然后根据关联关系得到相应的水果名,具体实现如下

- (void)test
{
    NSArray *fruits = @[@"banana",@"apple",@"pear",@"orange",@"avocado"];
    NSArray *scores = @[[NSNumber numberWithDouble:0.14],[NSNumber numberWithDouble:0.5],[NSNumber numberWithDouble:0.22],[NSNumber numberWithDouble:0.12],[NSNumber numberWithDouble:0.34]];
    NSMutableArray *sources = [[NSMutableArray alloc]init];//构造一个新的数据源
    
   double tempScore; //记录最高分数
    for (int i = 0; i < scores.count; i ++) {
        //将最大值保存到tempScore
        if (tempScore <= [scores[i] doubleValue]) {
            tempScore = [scores[i] doubleValue];
        }
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        //单独将score作为一个值存起来以便后续遍历获取到想要的节点
        [dic setObject:scores[i] forKey:@"score"];
        //将score和fruit匹配,根据score获取到最终fruit
        [dic setObject:fruits[i] forKey:scores[i]];
        [sources addObject:dic];
    }
    //遍历新数据源并根据最高分数获取到相对应的水果
    for (NSDictionary *dic in sources) {
        if ([[dic objectForKey:@"score"] doubleValue] == tempScore) {
            NSString *fruitName = [dic objectForKey:[NSNumber numberWithDouble:tempScore]];
            NSLog(@"分数最高的水果是:%@",fruitName);
            break;
        }
    }
}

思路二:既然两个数组的位置一一对应,那么只要知道最高分数在scores数组中的位置,fruits中所对应的这个位置也就是分数最高的水果了,具体实现如下:

- (void)test
{
    NSArray *fruits = @[@"banana",@"apple",@"pear",@"orange",@"avocado"];
    NSArray *scores = @[[NSNumber numberWithDouble:0.14],[NSNumber numberWithDouble:0.5],[NSNumber numberWithDouble:0.72],[NSNumber numberWithDouble:0.12],[NSNumber numberWithDouble:0.34]];
    
    double tempScore = [scores[0] doubleValue]; //记录最高分数
    int index;//记录位置
    for (int i = 1; i < scores.count; i ++) {
        //记录最大分数的位置
        if (tempScore <= [scores[i] doubleValue]) {
            tempScore = [scores[i] doubleValue];
            index = i;
        }
    }
    NSLog(@"分数最高的水果是:%@",fruits[index]);
}

补充:以上两种都是针对只有一个最大值的情况,要是出现多个相同的最大值呢?其实也简单的,只需要将所有最大值存到一个数组中就好,具体实现如下:

思路一补充:

- (void)test
{
    NSArray *fruits = @[@"banana",@"apple",@"pear",@"orange",@"avocado"];
    NSArray *scores = @[[NSNumber numberWithDouble:0.14],[NSNumber numberWithDouble:0.5],[NSNumber numberWithDouble:0.5],[NSNumber numberWithDouble:0.12],[NSNumber numberWithDouble:0.5]];
    NSMutableArray *sources = [[NSMutableArray alloc]init];//构造一个新的数据源
    
   double tempScore; //记录最高分数
    for (int i = 0; i < scores.count; i ++) {
        //将最大值保存到tempScore
        if (tempScore <= [scores[i] doubleValue]) {
            tempScore = [scores[i] doubleValue];
        }
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        //单独将score作为一个值存起来以便后续遍历获取到想要的节点
        [dic setObject:scores[i] forKey:@"score"];
        //将score和fruit匹配,根据score获取到最终fruit
        [dic setObject:fruits[i] forKey:scores[i]];
        [sources addObject:dic];
    }
    
    NSMutableArray *newFruits = [[NSMutableArray alloc]init];//存放相同分数的水果
    
    //遍历新数据源并根据最高分数获取到相对应的水果
    for (NSDictionary *dic in sources) {
        if ([[dic objectForKey:@"score"] doubleValue] == tempScore) {
            NSString *fruitName = [dic objectForKey:[NSNumber numberWithDouble:tempScore]];
            [newFruits addObject:fruitName];
        }
    }
    
    NSString *multiName = @"";
    for (NSString *newFruit in newFruits) {
        multiName = [multiName stringByAppendingString:[NSString stringWithFormat:@"%@,",newFruit]];
    }
    NSLog(@"分数最高的水果是:%@",multiName);
}

思路二补充:

- (void)test
{
    NSArray *fruits = @[@"banana",@"apple",@"pear",@"orange",@"avocado"];
    NSArray *scores = @[[NSNumber numberWithDouble:0.14],[NSNumber numberWithDouble:0.5],[NSNumber numberWithDouble:0.72],[NSNumber numberWithDouble:0.12],[NSNumber numberWithDouble:0.5]];
    
    double tempScore = [scores[0] doubleValue]; //记录最高分数
    int tempIndex;
    for (int i = 1; i < scores.count; i ++) {
        //记录最大分数的位置
        if (tempScore <= [scores[i] doubleValue]) {
            tempScore = [scores[i] doubleValue];
            tempIndex = i;
        }
    }
    NSMutableArray *newIndexs = [[NSMutableArray alloc]init];//记录相同分数的位置
    
    //将获取到的最大分数和数组中的每一个元素比较,相等的位置记录到位置数组中
    for (int i = 0; i < scores.count; i ++) {
        if (tempScore == [scores[i] doubleValue]) {
            [newIndexs addObject:[NSNumber numberWithInt:i]];
        }
    }
    
    NSString *multiName = @"";
    for (NSNumber *index in newIndexs) {
        multiName = [multiName stringByAppendingString:[NSString stringWithFormat:@"%@,",fruits[[index integerValue]]]];
    }
    NSLog(@"分数最高的水果是:%@",multiName);
}


你可能感兴趣的:(objective-c)