[英]Compare integer value with NSMutableArray and find maximum of that value
Suppose I have a integer number 10. and my NSMutableArray contains 1 to 20 values. 假设我有一个整数10。我的NSMutableArray包含1到20个值。 So I have to find maximum of 10 from NSMutableArray means the answer is 11. 所以我必须从NSMutableArray中找到最多10个,意味着答案是11。
I know how to find maximum number. 我知道如何找到最大数量。
int max = [[numbers valueForKeyPath:@"@max.intValue"] intValue];
But I don't know how to compare and find maximum number from array. 但是我不知道如何比较并从数组中找到最大数目。 I can use for loop to find but is there any faster solution like above? 我可以使用for循环来查找,但是有没有像上面这样的更快的解决方案?
NSArray *_timesArray = @[@1, @23, @57, @59, @120];
NSTimeInterval currentTime = 109;
NSInteger playerTime=currentTime;
NSUInteger index = [_timesArray indexOfObject:@(playerTime)
inSortedRange:NSMakeRange(0, _timesArray.count-1)
options:NSBinarySearchingFirstEqual | NSBinarySearchingInsertionIndex
usingComparator:^(id a, id b) {
return [a compare:b];
}];
NSLog(@"Index: %lu", (unsigned long)index);
your answer is 4; 您的答案是4;
Suppose your array like this. 假设您的数组是这样的。
NSMutableArray *numbers = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20", nil];
And declare your number like this. 并这样声明您的电话号码。
int max = 11;
int find;
Here max is the your static declaration. max是您的静态声明。 And find is the your result maximum number. 并找到您的结果最大数量。
for(int i = 0;i<numbers.count;i++)
{
if(max < [[numbers objectAtIndex:i]intValue])
{
find = [[numbers objectAtIndex:i]intValue];
NSLog(@"%d",find);
break;
}
}
Do this. 做这个。
Try this: 尝试这个:
NSMutableArray *numbers = [[NSMutableArray alloc] initWithObjects:@"20", @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11", @"12", @"13", @"14", @"15", @"16", @"17", @"18", @"19", @"20", nil];
int minimum = 10;
int nextNumber = [[numbers valueForKeyPath:@"@max.intValue"] intValue];
for(int i = 0;i<numbers.count;i++)
{
if([[numbers objectAtIndex:i] intValue] > minimum) {
if([[numbers objectAtIndex:i] intValue] < nextNumber) {
nextNumber = [[numbers objectAtIndex:i] intValue];
}
}
}
NSLog(@"Maxmimum number right after %d from whole array is: %d",minimum, nextNumber);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.