2011-05-11 4 views
1

3 개의 배열 중에서 가장 긴 (가장 높은 수) 배열을 찾으려면 어떻게합니까?가장 많은 수의 배열 찾으십시오.

배경 : I 매칭 함수는 잘 작동해야

- 사용 환경 containg 부울 값을 갖는 세 개의 사전, 물품은 세 가지 태그 카테고리를 가지고, 기능 검사 거세한 숫양 태그 A는, 태그 B가 켜져 사전 (A)에 온 사전 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 

사람이 멋진을 가지고하지 않는 한 나는 3 개 개의 입력이 길이의 배열이 될 수있는이 작업을 수행하는 가장 좋은 방법을

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

'[arrayX count]'는 배열에있는 원소의 수를 알려주기 때문에 찾고있는 것을 분명히 놓치고 있습니다. –

+0

죄송합니다 - 질문을 더 잘 이해하려고합니다. 기사 컬렉션을 검색하여 사용자 태그로 태그가 지정된 것을 찾으십니까? – nielsbot

+0

@neilsbot 배열 중에서 가장 긴 배열을 찾고 싶었습니다. 그러면 내 논리가 작동합니다. 나는 코드 덩어리가 누군가에게 유용 할 수 있으며, 게시 된 동안 누군가가 할 수있는 말을 할 수 있습니다 ... – JulianB

답변

1

각 배열의 모든 값을 확인하려면 nielsbot을 사용하십시오. 간단하게 삽입하십시오 for 루프 매개 변수에 포함시켜야합니다.

MAX(arrayA.count, MAX(arrayB.count, arrayC.count)) 

for(int i=0; i < MAX(arrayA.count, MAX(arrayB.count, arrayC.count)); i++) { 
    // Blah Blah Blah 
} 

MAX()는 두 개의 값 중 큰 값을 반환하므로 가장 높은 값을 쉽게 선택할 수 있습니다. 예제 코드에서 실제로 가장 긴 배열을 필요로하지 않는 것처럼 보입니다.

+0

사실, 그렇습니다. 나는 당신의 솔루션을 다시 읽었으며 그것은 의미가 있습니다. Ocmans 면도칼과 그 모든 것들. 고마워요! – JulianB

0

가장 많이 계산되는 배열을 찾으려면 내가 취할 방법이 될 것입니다.

NSArray *largestArray = arrayA; 
if ([largestArray count] < [arrayB count]) { 
    largestArray = arrayB; 
} 
if ([largestArray count] < [arrayC count]) { 
    largestArray = arrayC; 
} 

배열이 더 많은 경우이 방법이 최적의 방법은 아니지만 3면에서는 잘 수행해야합니다. 매번 가장 큰 배열을 반환해야합니다.

+1

아니면 MAX (arrayA.count, MAX (arrayB.count, arrayC. count))' – nielsbot

+0

오 죄송합니다 - 그것은 가장 높은 카운트이며, 가장 높은 카운트를 가진 배열이 아닙니다. – nielsbot

+0

좋은 해결책, 고마워. 심지어 내 for 루프, swcheeeet에 사용할 변수를 제공합니다. 내가 알지 못했지만 술수가 아닌 술어와 같은 마법 이었으면 좋겠다. – JulianB

관련 문제