2013-08-07 80 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);