如何查找哪個數組是三個數組中最長的(最高計數)?查找哪個數組的計數最高
背景:
我具有匹配功能運作良好 - 3個詞典與方含用戶偏好布爾值,製品具有三個標籤類,函數檢查閹標籤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
除非有人有一個真棒想法...
我明顯錯過了你要找的東西,因爲'[arrayX count]'給你數組中元素的數量。 – 2011-05-11 23:18:31
抱歉 - 試圖更好地理解問題。你正在搜索文章的集合,以找到哪些標籤用戶的標籤? – nielsbot 2011-05-11 23:23:39
@neilsbot我想找到最長的陣列選擇陣列。這將允許我的邏輯工作。我認爲代碼塊可能對某人有用,並且在發佈時,有人可能會說......你應該這樣做...... – JulianB 2011-05-12 00:11:12