Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何在NSMutableArray中定位最大值和位置_Objective C_Nsmutablearray_Key Value Coding - Fatal编程技术网

Objective c 如何在NSMutableArray中定位最大值和位置

Objective c 如何在NSMutableArray中定位最大值和位置,objective-c,nsmutablearray,key-value-coding,Objective C,Nsmutablearray,Key Value Coding,我需要在可变数组中搜索最大值,并返回其位置和值。我只想遍历数组一次,我不确定这是否可行 下面是一个我正在努力完成的例子 NSMutableArray *array = [[NSMutableArray alloc] init]; for (int i = 0; i<20; i++) [array addObject:[NSNumber numberWithInteger:(arc4random()%200)]]; NSObject *max = [

我需要在可变数组中搜索最大值,并返回其位置和值。我只想遍历数组一次,我不确定这是否可行

下面是一个我正在努力完成的例子

    NSMutableArray *array = [[NSMutableArray alloc] init];
    for (int i = 0; i<20; i++)
        [array addObject:[NSNumber numberWithInteger:(arc4random()%200)]];

    NSObject *max = [array valueForKeyPath:@"@max.self"];
NSMutableArray*array=[[NSMutableArray alloc]init];

对于(int i=0;i如果您不担心数组中可能存在多个相同的最大值,可以使用
-indexOfObject:
获取索引。它将返回数组中第一个出现的对象

NSUInteger index = [array indexOfObject:max];

使用
valueForKeyPath:@“@max.self”
非常好,但前提是您需要最大值

为了在一次迭代中同时知道索引和值,我将使用
enumerateWithBlock:

NSMutableArray *array = [[NSMutableArray alloc] init];
for (int i = 0; i<20; i++)
    [array addObject:[NSNumber numberWithInteger:(arc4random()%200)]];

__block NSUInteger maxIndex;
__block NSNumber* maxValue = [NSNumber numberWithFloat:0];
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    NSNumber* newValue = obj;
    if ([newValue isGreaterThan:maxValue]) {
        maxValue = newValue;
        maxIndex = idx;
    }
}];
NSMutableArray*array=[[NSMutableArray alloc]init];

对于(inti=0;i我希望,下面的解决方案将帮助您从数组中识别最大值

int max = [[numberArray valueForKeyPath:@"@max.intValue"] intValue];    
NSLog(@"Highest number: %i",max);

如果有任何问题,请告诉我。

在这里,您可以找到正确的方法来处理集合和KVC 马特写的这篇文章很好

max
对象没有位置,因为数组中的值可能不止一次出现。这就是我试图避免的:通过数组进行第二次迭代。我的示例有20个对象,我的实际代码有更多对象,并且正在定期重新处理。是否有特殊原因需要在也许你应该考虑以一种保持数组排序的方式插入对象,或者考虑使用一个不同的数据结构来允许最大值的查找(Max Head?)。但它是有效的…您还间接地解决了第二个问题,因此thanksA block是一个闭包,在其他编程语言中也可以使用。正如您所说,更多阅读!官方文档:@iPatel,如果您没有找到其他最佳解决方案,请不要编辑答案。您在前缀中添加了一些空格。b请不要这样做ngs@iPatel只是更正了格式。这没什么错。回答和编辑是有区别的。如果你有不同的解决方案,你应该把一个单独的答案。但是格式编辑是可以接受的,可以编辑。