【问题标题】:Min and Max Value of an NSMutableArrayNSMutableArray 的最小值和最大值
【发布时间】:2013-08-07 07:09:01
【问题描述】:

我有一段看起来很简单的代码让我完全摸不着头脑。

NSInteger ymax;
NSInteger ymin; 
NSInteger numberIndex1;
NSInteger numberIndex2;


for (NSNumber *theNumber in array2)
{
    if ([theNumber integerValue] > ymax) {
        ymax = [theNumber integerValue];
        numberIndex1 = [array2 indexOfObject:theNumber];
    }
}

for (NSNumber *theNumber in array2)
{
    if ([theNumber integerValue] < ymin) {
        ymin = [theNumber integerValue];
        numberIndex2 = [array2 indexOfObject:theNumber];
    }

} 

NSLog(@"Highest number: %d at index: %d", ymax, numberIndex1);
NSLog(@"Lowest number: %d at index: %d", ymin, numberIndex2);

NSLog输出为:

最高数:129171656 索引:-1073752392(嗯??)

最低数字:57,索引:5(正确)

你如何解释这种奇怪的行为?这两个功能看起来都一样。一个工作,一个不工作?我已经玩过很多次了,但我仍然无法将手指放在它上面。任何帮助将不胜感激/

【问题讨论】:

  • 你初始化了 NSIntegers 了吗?
  • 按照上面的代码初始化它。
  • 数组中的数字有多大?
  • 抱歉,我没看到你在哪里给 NSIntegers 初始值...
  • @JohnWoods 数组很小。大约 12 个元素,最大的是 3 位数字。 @Raspu 奇怪的是,它似乎在没有任何初始值的情况下工作。但即使我确实尝试使用数组的第一个元素像 NSInteger ymin = [array2 indexOfObject:0]; 那样初始化它,我也得到了相同的结果。

标签: iphone ios objective-c nsmutablearray


【解决方案1】:

您可以通过以下代码获取最大和最小数量。可能对你有帮助

NSNumber * max = [array2 valueForKeyPath:@"@max.intValue"];
NSNumber * min = [array2 valueForKeyPath:@"@min.intValue"];
NSUInteger numberIndex1 = [array indexOfObject:min];
NSUInteger numberIndex2 = [array indexOfObject:max];

NSLog(@"Max Value = %d and index = %d",[max intValue],numberIndex1);
NSLog(@"Min Value = %d and index = %d",[min intValue],numberIndex2);

【讨论】:

  • indexOfObject: 需要一个对象,而不是浮点/整数或任何其他基本数据类型。
  • @RahulPatel 由于您在valueForKeyPath: 中使用.intValue,如果NSNumber 的实际值超出整数范围,则逻辑将中断。
  • @AmreshKumar 你是对的。这就是为什么我一开始没有采用这种方法的原因。我的代码更长,但对我来说效果更好。
  • @AmreshKumar:你是对的,但提问者没有定义哪些是值,这就是为什么我提到代码只是为了想法。不用于复制粘贴
【解决方案2】:

如果我没看错,你认为 NSInteger 的默认值为 0,不,它不能保证为零,因为它是一个局部自动变量。没有初始化,它的值是不确定的。

所以你需要为你的 var 设置默认值,从 ymax = -1 开始;

【讨论】:

  • 你说得对,这就是问题所在。但是在这种情况下,ymax = 0;工作正常,但 ymin 需要使用数组的第一个元素进行初始化,否则它返回 null/
  • 不一定,设置ymin = NSIntegerMax;
  • 我没有意识到这一点。效果也很好,谢谢。 This 帮助。
【解决方案3】:

请初始化 NSInteger ymax = 0; NSInteger ymin = 0 ; NSInteger numberIndex1 = 0; NSInteger numberIndex2 = 0; 它将解决您的问题。 否则它会检查垃圾值并给出错误的结果。

【讨论】:

  • 这解决了我获取最大值的问题。但是现在最小值被搞砸了。最高数字:491 索引:2 最低数字:0 索引:0 但我认为像 NSInteger ymin = [array2 indexOfObject:0]; 这样初始化应该可以解决这个问题。
  • @nikhitadkslfslg 是的。在最大情况下你也可以这样做。
【解决方案4】:

享受我的回答......快乐的编码

NSArray *arr1=[[NSArray alloc]initWithObjects:@"0.987",@"0.951",@"0.881",@"0.784",@"0.662",@"0.522",@"0.381",@ "-0.265",@"-0.197", @"0.189",@"-0.233",@"0.310",@"0.402",@"0.402",@"0.988",@"0.633",@"0.661",@"0.656",@"0.617" ,@"0.634",@"0.690",@"0.767",@"0.836",nil];

NSNumber * max = [arr1 valueForKeyPath:@"@max.floatValue"];
NSString *str=[NSString stringWithFormat:@"%@",max];
NSInteger path=[arr1 indexOfObject:str];
NSIndexPath *indepath=[NSIndexPath indexPathForItem:path inSection:0];

NSNumber * min = [arr1 valueForKeyPath:@"@min.floatValue"];
 NSString *str1=[NSString stringWithFormat:@"%@",min];
NSInteger path1=[arr1 indexOfObject:str1];
NSIndexPath *indepath1=[NSIndexPath indexPathForItem:path1 inSection:0];


NSLog(@"Max Value = %f and index = %ld",[max floatValue],(long)indepath.row);
NSLog(@"Min Value = %f and index = %ld",[min floatValue],(long)indepath1.row);

【讨论】:

    【解决方案5】:

    更合理的解决方案是:

    
    NSArray *originalArray = @[[NSNumber numberWithInt:91],
                                   [NSNumber numberWithInt:12],
                                   [NSNumber numberWithInt:99123],
                                   [NSNumber numberWithInt:9],
                                   [NSNumber numberWithInt:43234]];
    NSArray *sortedArray = [originalArray sortedArrayUsingSelector:@selector(compare:)];
    NSNumber *minNumber = [sortedArray objectAtIndex:0];
    NSNumber *maxNumber = [sortedArray lastObject];
    NSInteger minIndex = [originalArray indexOfObject:minNumber];
    NSInteger maxIndex = [originalArray indexOfObject:maxNumber];
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 2016-04-10
    • 2019-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多