如何找到三个阵列中哪个阵列最长(最高计数)?
背景:
我有一个匹配的函数运行良好 - 三个字典,其中布尔值包含用户首选项,一篇文章有三个标记类别,函数检查标记A在字典A中打开,标记B在字典B中打开等等
现在要求标签A中有N个条目,标签B中有N个条目等等
因此,三个标签阵列中的每一个都可以有不同的长度,我能想到的最简单的方法是从ArrayA,ArrayB和ArrayC找到最长的数组(包含大多数条目)
这是我原来的工作循环
for (id myArrayElement in storyArray) {
NSString *myString = [NSString stringWithString:[myArrayElement industryA]];
NSString *myIssue = [NSString stringWithString:[myArrayElement issueA]];
NSString *myService = [NSString stringWithString:[myArrayElement serviceA]];
if (
[prefsDictionary valueForKeyPath:[NSString stringWithFormat:@"Industries.%@", myString]] ||
[prefsDictionary valueForKeyPath:[NSString stringWithFormat:@"Issues.%@", myIssueElement]] ||
[prefsDictionary valueForKeyPath:[NSString stringWithFormat:@"Services.%@", myService]]
) {
// One of the story's tags matches a key in one of the corresponding dictionaries
// Look up what this preference is set to
NSString *keyvalue = [[prefsDictionary valueForKey:@"Industries"] valueForKey:myString];
NSString *Issuesvalue = [[prefsDictionary valueForKey:@"Issues"] valueForKey:myIssueElement];
NSString *Servicevalue = [[prefsDictionary valueForKey:@"Services"] valueForKey:myService];
if (
[keyvalue isEqualToString:@"1"] ||
[Issuesvalue isEqualToString:@"1"] ||
[Servicevalue isEqualToString:@"1"]
) {
// It's a match, add the story
[self.favList addObject:myArrayElement];
}
} // prefsDictionary End if
我正在考虑这样做的最好方法,其中三个输入可以是任意长度的数组
for (id myArrayElement in delegate.storyArray) {
NSArray *industyArr = [[myArrayElement industryA] componentsSeparatedByString:@"|"];
NSArray *issueArr = [[myArrayElement issueA] componentsSeparatedByString:@"|"];
NSArray *serviceArr = [[myArrayElement serviceA] componentsSeparatedByString:@"|"];
// We need to find longest array
// Pad the shorter arrays, or use if ([array count] >= 4) {id obj = [scores objectAtIndex:3];}
// Then loop using the largest array length
for (loop longest array length) {
// get nth entry in industyArr... thisIndustry
// get nth entry in issueArr... thisIssue
// get nth entry in serviceArr... thisService
if (
[prefsDictionary valueForKeyPath:[NSString stringWithFormat:@"Industries.%@", thisIndustry]] ||
[prefsDictionary valueForKeyPath:[NSString stringWithFormat:@"Issues.%@", thisIssue]] ||
[prefsDictionary valueForKeyPath:[NSString stringWithFormat:@"Services.%@", thisService]]
) {
// One of the story's tags matches a key in one of the corresponding dictionaries
NSString *keyvalue = [[prefsDictionary valueForKey:@"Industries"] valueForKey:thisIndustry];
NSString *Issuesvalue = [[prefsDictionary valueForKey:@"Issues"] valueForKey:thisIssue];
NSString *Servicevalue = [[prefsDictionary valueForKey:@"Services"] valueForKey:thisService];
if (
[keyvalue isEqualToString:@"1"] ||
[Issuesvalue isEqualToString:@"1"] ||
[Servicevalue isEqualToString:@"1"]
) {
// It's a match, add the story
[self.favList addObject:myArrayElement];
// EXIT THE INNER LOOP NOW WE HAVE A MATCH
}
} // prefsDictionary End if
} // End myIssueElement for
} // End myArrayElement for
除非有人有一个很棒的主意......
答案 0 :(得分:1)
如果您只是想确保查看每个数组中的所有值,我实际上会支持nielsbot。只需插入
MAX(arrayA.count, MAX(arrayB.count, arrayC.count))
进入你的for循环参数应覆盖它。
for( int i=0; i < MAX(arrayA.count, MAX(arrayB.count, arrayC.count)); i++ ) {
// Blah Blah Blah
}
MAX()返回两个值中较大的一个,使您可以轻松选出最高计数。从您的示例代码中,您似乎并不需要最长的数组,只需要最高数量。
答案 1 :(得分:0)
如果您要做的就是找出哪个数组的计数最高,那么这就是我要采取的方法:
NSArray *largestArray = arrayA;
if ([largestArray count] < [arrayB count]) {
largestArray = arrayB;
}
if ([largestArray count] < [arrayC count]) {
largestArray = arrayC;
}
如果有更多的阵列,这不是最佳方法,但只有三个,它应该做得很好。这应该每次返回最大的数组