2013-08-07 74 views
2

我有一个看起来很简单的代码,让我完全失望。NSMutableArray的最小值和最大值

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(正确)

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

+0

你初始化了NSIntegers吗? – Raspu

+0

按照上面的代码中所述初始化它。 – geekchic

+0

数组中的数字有多大? – Woodstock

回答

1

如果我没有错,你正在考虑NSInteger的默认值是0,不,它不能保证为零,因为它是一个本地自动变量。没有初始化,它的值是不确定的。

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

+0

你是对的,这是问题所在。但是在这种情况下,ymax = 0;工作得很好,但ymin需要用数组的第一个元素初始化或返回null/ – geekchic

+0

不一定,set ymin = NSIntegerMax; – null

+0

我没有意识到这一点。非常好,谢谢。 [这](http://stackoverflow.com/questions/4800015/what-is-the-maximum-value-of-nsinteger)帮助。 – geekchic

2

您可以获得最大和最小数量如下代码。它可能会帮助你

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); 
+1

'indexOfObject:'需要一个对象而不是float/int或任何其他基本数据类型。 –

+0

请看我编辑的答案 –

+1

@Amresh Kumar非常感谢..... –

1

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

+0

这解决了我获得最大值的问题。但是现在这个分钟已经被打乱了。 最高数:491在指数:2 最低数量:0在指数:0 但我认为初始化像这样'NSInteger的YMIN = [数组2 indexOfObject:0];'应该解决这个问题。 – geekchic

+0

@nikhitadkslfslg是的。在最大的情况下,你也可以做。 – Subrat

-3

一个更合理的解决办法是:


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]; 
+1

备注:对于大型数组,排序它只是为了找到最小值和最大值比列举数组或键值编码方法慢*,请比较http://stackoverflow.com/a/15931719/1187415。 –

+0

@MartinR感谢您的评论。我会记住这一点。 –

0

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

的NSArray * ARR1 = [[NSArray的页头] 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”,零]。

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);